Local Removal of Merged Git Branches

When working with several repositories and feature branches, it can get tedious to clean-up merged and removed branches by hand. A small snippet can do the job for us automatically.

The following snippet takes all merged branches, except develop and master.

The output is written to a temporary file called “merged-branches “.

This file is loaded with vim where can review the list of branches which will be locally removed afterwards.

git branch --merged | grep -v 'master' | grep -v 'develop' >merged-branches && vim merged-branches && xargs git branch -d <merged-branches && rm merged-branches

In case you do not need the additional manual verification step, you can omit the temporary file.

git branch --merged | grep -v 'master' | grep -v 'develop' && xargs git branch -d

The script throws an error in case there are no branches to delete.