Skip to content

Instantly share code, notes, and snippets.

@bxt
Last active June 23, 2021 15:01
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bxt/566fd7de151bf73a937d to your computer and use it in GitHub Desktop.
Save bxt/566fd7de151bf73a937d to your computer and use it in GitHub Desktop.
Git Delete Merged Branches
#!/bin/bash
MAIN=${1:-development}
BRANCHES=$(git branch --merged $MAIN | grep -v -e 'master\|staging\|development\|\*')
echo Branches merged into $MAIN:
echo $BRANCHES
read -p "Delete these branches (y/n)? " answer
if [ "$answer" = "n" ]; then echo aborting && exit; fi
echo $BRANCHES | xargs -n 1 git branch -d

Git Delete Merged Branches

This is a handy script to delete any leftover and merged git branches. It will detect branches merged into development and delete those, except branches named master or staging. These branch names can be configured by editing the script.

To install simply run:

curl -L https://git.io/vVHF6 | bash

Or manually put this script into ~/bin and run chmod +x ~/bin/git-deletebranches.sh.

If you run it the output will look like this:

~/my-git-repo> git-deletebranches.sh
Branches merged into development:
some-merged-feature-branch another-merged-feature-branch
Delete these branches (y/n)? y
Deleted branch some-merged-feature-branch (was 281d690).
Deleted branch another-merged-feature-branch (was 0b67c29).

You can also supply a branch name as a first argument, if your target branch is not named development like this:

~/my-git-repo> git-deletebranches.sh master
Branches merged into master:

[...]

Enjoy! :)

#!/bin/sh
mkdir -p ~/bin
curl https://gist.githubusercontent.com/bxt/566fd7de151bf73a937d/raw/git-deletebranches.sh > ~/bin/git-deletebranches.sh
chmod +x ~/bin/git-deletebranches.sh
echo git-deletebranches.sh installed successfully.
@bxt
Copy link
Author

bxt commented Oct 19, 2015

For OS X you have to remove the -r flag from xargs, as this is implied for BSDish versions.

@bxt
Copy link
Author

bxt commented Oct 26, 2016

This script already saved me today. I had pushed a feature branch to the remote repository, had the merge-request accepted and ran git-deletebranches.sh as I always do after my MRs got merged. However, one of the merged branches was not listed. At first I thought I had encountered a bug in git-deletebranches.sh but then it became clear: I had forgotten to push one commit in this branch to origin, so the branch was not fully merged. I ran:

git checkout feature-branch-with-leftover-commit
git checkout -b new-branch-for-single-commit
git push origin new-branch-for-single-commit

And when this branch got merged in the remote I did:

git checkout developement
git fetch -p
> From git.foo.de:Bar/Baz
>  - [deleted]         (none)     -> origin/new-branch-for-single-commit
> (...)
git pull
git-deletebranches.sh 
> Branches merged into development:
> feature-branch-with-leftover-commit new-branch-for-single-commit
> Delete these branches (y/n)? 
> Deleted branch feature-branch-with-leftover-commit (was d3adbef).
> Deleted branch new-branch-for-single-commit (was b31337a).

Yey, everything in check again.

@sshleifer
Copy link

sshleifer commented Jun 9, 2020

I am working in a fork of a repo and sending prs to an upstream repo is there a way to delete branches that have been merged into the upstream repo? Reposted this on SO

@bxt
Copy link
Author

bxt commented Jul 5, 2020

@sshleifer I think it should work just fine, if you keep your local "main" branch (e.g. master) up to date with the upstream repo, e.g. you can use git remove add upstream [repourl] and then git pull upstream master to refresh you local branch. I would also recommend to keep your fork up to date. Then the script would use that to check wether branches are merged. I have to admit I don't use this script anymore since we often sqaush-merge now 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment