Git: Delete merged branches

published on in category Git , Tags: Git VCS Version Control

When using Git extensively, you’ll likely have multiple branches which need to be cleaned up from time to time. In some repositories I have hundreds of feature/, hotfix/ and release/ branches, which are already merged to the master branch and deleting them manually would be a mess.

So here’s how to delete all branches from the server, that are already merged to the master branch. In this case, branches need to start with either feature/, hotfix/ or release/ but you can simply adjust this to match your needs.

git remote update origin --prune
git checkout master
git pull origin master
git branch -r --merged master | grep -E 'feature\/|hotfix\/|release\/' | sed 's/origin\///g' | xargs git push origin --delete

I run this site without advertisement of any kind. All information is free and my only goal is to give back something to the amazing free software development community. If you find some value in this, please consider donating me a cup of coffee using PayPal. Thank you so much!