Pruning Git branches


TL;DR: You can delete all local branches merged into master with git branch --merged master | grep -v master | xargs git branch -d

You can list all local branches that are already merged into master:

$ git branch --merged master

From the list, you can grep all branches except master:

$ git branch --merged master | grep -v master

Then delete all local branches that have been merged to master:

$ git branch --merged master | grep -v master | xargs git branch -d

If you want to delete branches merged into dev while excluding master:

$ git branch --merged dev | grep -v 'master\|dev' | xargs git branch -d
# or
$ git branch --merged dev | grep -v -e master -e dev | xargs git branch -d