When I’m working on a project using Git, I’m good about deleting local branches that I don’t need anymore. However, I often forget about my remote branches after they are merged. While it is convenient that most of the Git hosting I use makes it easy to show only unmerged branches, this also makes it easier to forget about these merged branches.
Eventually, I find I do need to clean up after myself and rather than manually going through all these branches and delete them, I’ve found the following command will delete all branches on the remote
origin that have been merged into
git branch --all --merged remotes/origin/master | grep --invert-match master | grep --invert-match HEAD | grep "remotes/origin/" | cut -d "/" -f 3- | xargs -n 1 git push --delete origin
This answer is a modified version of this Stack Overflow Answer.
While this is a really helpful command, it is too long to type out each time. Fortunately, Git allows use to create an alias.
We can add the alias to our global configuration by running the following line:
git config --global alias.delete-merged-on-origin '!git branch --all --merged remotes/origin/master | grep --invert-match master | grep --invert-match HEAD | grep "remotes/origin/" | cut -d "/" -f 3- | xargs -n 1 git push --delete origin'
Now, whenever we type
git delete-merged-on-origin our command will be run.
How the Alias Works
git config --global line above writes this new alias to your
.gitconfig file (which is found at
~/.gitconfig on a Mac).
Another Option to Setup the Alias
git delete-merged-on-origin command is one of the commands available with the Iron Code Studio Git Configuration I use. By setting up this project, this command and many other time saving features I use are available.
Explanation of the Command
git branch --all --merged remotes/origin/master
List both remote-tracking branches and local branches. (See git branch –all).
Show only branches that have been merged into
remotes/origin/master. (See git branch –merged)
grep --invert-match master
Take the results thus far and remove any lines that match “master”. (See grep Matching Control)
grep --invert-match HEAD
Take the results thus far and remove any lines that match “HEAD”. (See grep Matching Control)
Take the results thus far and include only those lines that match “remotes/origin/”. (See grep Matching Control)
cut --d "/" -f 3-
Divide each line at the
/ character (
Keep only the third section and anything after that (i.e. remove sections 1 and 2) (
(See cut Documentation).
xargs -n 1 git push --delete origin
Take each line we currently have and use it as the argument (i.e. put it at the end) of the command.
git push --delete origin
Each line contains the name of a branch, so each of the branches listed are deleted from
Each line should be used only up to the first space (i.e. Use at most 1 argument per command line.) (
Photo by sacks08 on flickr
Anurag Goel says
I was looking for something like this to clear my remote branches after migrating my project from BitBucket to GitHub repo. Found it very useful in my case, just that double hyphen (–) in command “cut –d “/” -f 3-” is not supported on my mac, instead used single hyphen (-).