Last Updated: February 25, 2016
·
1.457K
· koen

Git branch and remote cleanup alias

To easily remove git branches and prune orphaned remotes I've created a alias named branch-cleanup:

First I fetch the remote origin with --prune, to remove references to deleted branches on the remote:

git fetch origin --prune

Then I list the branches which are merged to origin/master, I exclude the master branch and pass that list to git branch -d using xargs:

git branch --merged origin/master | grep -v 'master$' | xargs git branch -d

To create an alias for this, run the following:

git config --global alias.branch-cleanup "\!git fetch origin --prune && git branch --merged origin/master | grep -v 'master$' | xargs git branch -d"