How Can I Delete Multiple Branches That Have Been Merged?

Is there an easy way to delete all merged branches instead of deleting them one by one using git branch -D \<branchName\>

1 Like

Warning** :** While the recommendations here aren’t inherently destructive (since they are only deleting merged branches), recovering branches you didn’t mean to delete can be complicated and time-consuming. Use these techniques at your own risk!

Depending on the branching strategy in your repository, this can be fairly simple or somewhat complicated. If you want to delete all branches that have been merged into master then you can use this one-liner on macOS or Linux (it should also work on Windows in one of the various Unix shell extensions):

git branch --merged master |grep -v -e master| xargs -n 1 git branch -d

I have a Ruby script that I use for this that is a little more flexible and has some nice options. It was based off of a Gist I found for the same thing.

I hope that helps!

2 Likes

Thanks for the great information. This helps a lot! 

2 Likes

I just realized that I haven’t been deleting my branches as I go when merging PRs. This has been an oversight for a long while, so I now have a huge set of branches that I’d like to delete. Is it possible to bulk delete branches? Or do I have to delete them all one by one?

Hey @happydogcode, this is a great question and one that was asked in a previous topic. You can find a solution here.

1 Like

Thank you so much! I will look there.