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