I have many Git branches. How do I delete branches which have already been merged? Is there an easy way to delete them all instead of deleting them one by one?
I've used Adam's answer for years now. That said, that there are some cases where it wasn't behaving as I expected:
1 & 2 were straightforward to address, with just a change to the regex.
3 depends on the context of what you want (i.e. only delete branches that haven't been merged into master or against your current branch).
4 has the potential to be disastrous (although recoverable with git reflog
), if you unintentionally ran this in detached HEAD state.
Finally, I wanted this to all be in a one-liner that didn't require a separate (Bash|Ruby|Python) script.
Create a git alias "sweep" that accepts an optional -f
flag:
git config --global alias.sweep '!git branch --merged $([[ $1 != "-f" ]] \
&& git rev-parse master) | egrep -v "(^\*|^\s*(master|develop)$)" \
| xargs git branch -d'
and invoke it with:
git sweep
or:
git sweep -f
It was easiest for me to create an example git repo with some branches and commits to test the correct behavior:
mkdir sweep-test && cd sweep-test && git init
echo "hello" > hello
git add . && git commit -am "initial commit"
git branch foo && git branch bar && git branch develop && git branch notmaster && git branch masterful
git branch --list
bar develop foo * master masterful notmaster
The original regex misses the branches "masterful" and "notmaster" :
git checkout foo
git branch --merged | egrep -v "(^\*|master|dev)"
bar
With the updated regex (which now excludes "develop" rather than "dev"):
git branch --merged | egrep -v "(^\*|^\s*(master|develop)$)"
bar masterful notmaster
echo "foo" > foo
git add . && git commit -am "foo"
git checkout -b foobar
echo "foobar" > foobar
git add . && git commit -am "foobar"
My current branch is foobar, and if I re-run the above command to list the branches I want to delete, the branch "foo" is included even though it hasn't been merged into master:
git branch --merged | egrep -v "(^\*|^\s*(master|develop)$)"
bar foo masterful notmaster
However, if I run the same command on master, the branch "foo" is not included:
git checkout master && git branch --merged | egrep -v "(^\*|^\s*(master|develop)$)"
bar masterful notmaster
And this is simply because git branch --merged
defaults to the HEAD of the current branch if not otherwise specified. At least for my workflow, I don't want to delete local branches unless they've been merged to master, so I prefer the following variant:
git checkout foobar
git branch --merged $(git rev-parse master) | egrep -v "(^\*|^\s*(master|develop)$)"
bar masterful notmaster
Relying on the default behavior of git branch --merged
has even more significant consequences in detached HEAD state:
git checkout foobar
git checkout HEAD~0
git branch --merged | egrep -v "(^\*|^\s*(master|develop)$)"
bar foo foobar masterful notmaster
This would have deleted the branch I was just on, "foobar" along with "foo", which is almost certainly not the desired outcome. With our revised command, however:
git branch --merged $(git rev-parse master) | egrep -v "(^\*|^\s*(master|develop)$)"
bar masterful notmaster
git branch --merged $(git rev-parse master) | egrep -v "(^\*|^\s*(master|develop)$)" | xargs git branch -d
git config --global alias.sweep '!git branch --merged $([[ $1 != "-f" ]] \
&& git rev-parse master) | egrep -v "(^\*|^\s*(master|develop)$)" \
| xargs git branch -d'
The alias accepts an optional -f
flag. The default behavior is to only delete branches that have been merged into master, but the -f
flag will delete branches that have been merged into the current branch.
git sweep
Deleted branch bar (was 9a56952). Deleted branch masterful (was 9a56952). Deleted branch notmaster (was 9a56952).
git sweep -f
Deleted branch foo (was 2cea1ab).