Skip to content

Git: How to delete all the merged branches

Posted on:February 18, 2016 at 08:49 PM

Who has a repository without dead already merged branches? Admit it, almost no one. In my team, especially since we decided to move to gitflow[1], this ideal situation rarely happens. It is not that uncommon forget to delete a merged branch from remote, it happens to everyone. But

I believe a tidy repository should give you the possibility to have a look at its status just with a glance.

After some research I ended up with the following command to delete all your merge branches in one go:

git branch -r --merged |
grep origin |
grep -v '>' |
grep -v main |
xargs -L1 |
cut -d"/" -f2- |
xargs git push origin --delete

PS: Be careful to use the correct branch as reference, once I deleted develop by mistake :D

  1. Unfortunately git flow doesn’t take care of deleting the branches when merged. This behaviour could be a good thing if you love to try to fuck up your work playing with rebase and squash (like me). ↩︎