Last Updated: February 25, 2016
·
429
· klj613

Delete merged branches

Sometimes you have a huge list of merged branches.

git checkout master
git branch -d $(git branch --merged | sed '/* /d' | sed 's/^..//' | paste -sd " ")

What does it do?

type git branch --merged

sed '* /d' will remove the branch your on

sed 's/^..//' will remove the two spaces in front of each branch

paste -sd " " will put all the lines onto one line separated by a single space.

git branch -d delete the branches.

Note

git branch --merged will actually list any branches which points to a commit within your current branch. If you type git branch foo the foo branch will actually reference a commit within your current branch so git will mark it as "merged".