Git: Cleaning up merged branches

Using some kind of git branching strategy (e.g. GitFLow, GitHub Flow or other) normally produces quite a lot of branches. When these do not get merged on you machine, you end up with a lot of stranded branches locally, which can be annoying both when using console or some GUI.

Here is a simple way of cleaning these branches in bulk, just add this into your git config by running git config -e --global.

1
2
[alias]
    cleanup = "!git branch --merged | grep  -v '\\*\\|master\\|develop' | xargs -n 1 git branch -d"

Getting rid of stranded branches is then just a matter of running git cleanup in your repository.

There are different flavors of doing this, the one above checks if branch was merged into master or develop, you can adjust it for your needs. Check this Stack Overflow thread for more info and discussion.