Last Updated: September 03, 2018
·
13.44K
· railsbros-dirk

Show merged and unmerged branches in git

Maybe you are sometimes as lazy as I am and don't clean your git branches after you merged them. Maybe some other team members forgot to clean up. Maybe some monkeys came along created a whole bunch of branches and didn't tell anyone. Whatever the reason sometime you maybe just want to know what is merged and what is not. Just add the following aliases to your git config and you will get a list of branches with their last author and last commit date:

unmerged = !git branch -r --no-merged | grep -v HEAD | xargs -L1 git --no-pager log --pretty=tformat:'%Cgreen%d%Creset - %h by %an (%Cblue%ar%Creset)' -1

merged = !git branch -r --merged | grep -v HEAD | xargs -L1 git --no-pager log --pretty=tformat:'%Cgreen%d%Creset - %h by %an (%Cblue%ar%Creset)' -1

Note: each of the aliases must be on one line

Bonus

If you want to remove merged or unmerged branches from the remote you can run this command:

git branch -r --merged | grep -v master | \
ruby -ne '`git push origin #{$_.gsub("origin/", ":")}`'

2 Responses
Add your response

Made a few modification to have the output a bit easier to read (using the column command)

unmerged = !git branch -r --no-merged | grep -v HEAD | xargs -L1 git --no-pager log --pretty=tformat:'%Cgreen%d%Creset | %h | %an | %Cblue%ar%Creset' -1 | column -t -s '|'
merged = !git branch -r --merged | grep -v HEAD | xargs -L1 git --no-pager log --pretty=tformat:'%Cgreen%d%Creset | %h | %an | %Cblue%ar%Creset' -1 | column -t -s '|'

over 1 year ago ·

Thanks for the improvement. I will definitely integrate that in my config.

over 1 year ago ·