Last Updated: November 08, 2016
·
8.034K
· reinh

Show git branches sorted by latest commit

Note: coderwall's markdown pre-processor seems to think that -- indicate Haskell comments in the code blocks and is hiding them. I'm going to not use code blocks until it works. Sorry about that.

git config --global alias.latest "for-each-ref --sort=-committerdate --format='%(committerdate:short) %(refname:short)'"

Usage:

$ cd ~/Code/rails/rails && git latest

2012-11-19 origin/master
2012-11-19 origin/HEAD
2012-11-19 master
2012-11-19 origin/3-2-stable
2012-11-03 origin/encrypted_cookies
2012-11-03 origin/attributes_perf
... snipped ...

Show local branches only:

git config --global alias.latest "for-each-ref --sort=-committerdate refs/heads --format='%(committerdate:short) %(refname:short)'"

Source: http://stackoverflow.com/questions/5188320/how-can-i-get-a-list-of-git-branches-ordered-by-most-recent-commit

5 Responses
Add your response

By using committerdate:relative you can get:

2 days ago master
11 days ago origin/master
5 weeks ago tokens-feature
5 weeks ago origin/tokens-feature
5 weeks ago origin/login-feature
5 weeks ago login-feature
6 weeks ago service-feature
6 weeks ago origin/service-feature
6 weeks ago identity-feature
6 weeks ago origin/identity-feature
6 weeks ago origin/accounts-feature
6 weeks ago accounts-feature
over 1 year ago ·

Unfortunately for-each-ref's --format is crippled and you cannot use colors inside (in git log you can specify %C(yellow)string%C(reset)) :( anyone got around it maybe?

over 1 year ago ·

That would be cool to have it colored. Anyone?

over 1 year ago ·

To get colors use --shell option, like this:

git for-each-ref --sort=-committerdate --shell --format='echo -e "\e[0031m" %(refname:short) "\e[00m\e[0032m" %(committerdate:relative) "\e[00m\e[0034m" %(committername) "\e[00m"' refs/heads/ refs/remotes | bash
over 1 year ago ·

I throw a "objectname:short" in the format too so there's a handy sha if you need it

over 1 year ago ·