Where developers come to connect, share, build and be inspired.


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)'"


$ 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


  • Fc5f974ef4fd49e1bb6a48c669b496ef

    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
  • 68b3c1e2b3c882daab62c80db52a70ed

    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?

  • User-avatar

    That would be cool to have it colored. Anyone?

  • 20842946074d9b78d6004bb7cb18c603

    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

Add a comment