Last Updated: February 25, 2016
· lucianosousa

Fastest way to delete all merged branches in master (or any other branch)

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

2 Responses
Add your response


This assumes you have all branches checked out. "git branch -r --merged" will give you all remote branches that are merged in to whatever your current branch is. Also, depending on your branching strategy and how you deal with bugs or hotfixes, you may want to filter on how long it's been since the branch was touched.

Why the "grep -v dev"?

over 1 year ago ·

Thanks for the answer @adevore3.

So, I just removed the grep -v dev. There is no nonse at all. Sorry about that.

About the previously question.
I always run this command on master branch.

About the bugs and hotfixes branches, I just merge it when it's fully tested.
I mean, I run this command always to remove branches I forgot to.

over 1 year ago ·
Filed Under