Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Moderator
Message 1 of 6

How Can I Delete Multiple Branches That Have Been Merged?

Solved! Go to Solution.

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

5 Replies
Solution
Community Manager
Message 2 of 6

Re: How Can I Delete Multiple Branches That Have Been Merged?

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!

Moderator
Message 3 of 6

Re: How Can I Delete Multiple Branches That Have Been Merged?

Thanks for the great information. This helps a lot! 

Copilot Lvl 3
Message 4 of 6

Too many branches

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?

Moderator
Message 5 of 6

Re: Too many branches

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

Copilot Lvl 3
Message 6 of 6

Re: Too many branches

Thank you so much! I will look there.