Sometimes in the rush of the work day your git repos can get pretty littered with branches you just don’t need anymore. Here’s a few tricks to clean up your repos.

Local Branches Link to heading

Count how many local branches have already been merged into master:

git checkout master
git branch -a --merged | grep -v 'remotes/' | grep -v '\*' | grep -v master | wc -l

Remove all local git branches that have been merged into master:

git checkout master
git branch -a --merged | grep -v 'remotes/' | grep -v '\*' | grep -v master | xargs git branch -D

For a more detailed explanation checkout this Explain Shell.

Specific Remote Branches Link to heading

Count merged branches for a specific remote

git checkout master
git branch -a --merged | grep 'remotes/origin | wc -l

Remove remote git branches from a particular remote

git checkout master
git branch -a --merged | grep 'remotes/origin' | xargs -I {} git push origin :{}

Replace origin with any other remote name you have.

Explain Shell

All Remote Branches Link to heading

Count merged branches for all remotes

git branch -a --merged | grep 'remotes/' | wc -l

Remove all remote git branches that have been merged into master:

git checkout master
git branch -a --merged | grep 'remotes/' | xargs -I {} git push origin :{}

Explain Shell