Sort git Branches by Date

By  on  

I'll be first person to admit I don't do as much git repository maintenance as I should.  I rarely delete branches which have been merged, so a git branch execution shows me a mile-long list of branches that likely aren't relevant.  The best way to find branches I've recently used is to use the following command:

git for-each-ref --sort=-committerdate refs/heads/

The command above lists the most recently worked on branches from top to bottom.  If you want to see the date of last commit, you can do this:

git for-each-ref --sort='-committerdate' --format='%(refname)%09%(committerdate)' refs/heads | sed -e 's-refs/heads/--'

I find these commands incredibly helpful when returning to work from a weekend or just jumping from project to project.  Hopefully you can use these commands too!

Recent Features

  • By
    Send Text Messages with PHP

    Kids these days, I tell ya.  All they care about is the technology.  The video games.  The bottled water.  Oh, and the texting, always the texting.  Back in my day, all we had was...OK, I had all of these things too.  But I still don't get...

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

Incredible Demos

  • By
    NSFW Blocker Using MooTools and CSS

    One of my guilty pleasures is scoping out the latest celebrity gossip from PerezHilton.com, DListed.com, and JoBlo.com. Unfortunately, these sites occasionally post NSFW pictures which makes checking these sites on lunch a huge gamble -- a trip to HR's office could be just a click away. Since...

  • By
    MooTools-Like Element Creation in jQuery

    I really dislike jQuery's element creation syntax. It's basically the same as typing out HTML but within a JavaScript string...ugly! Luckily Basil Goldman has created a jQuery plugin that allows you to create elements using MooTools-like syntax. Standard jQuery Element Creation Looks exactly like writing out...

Discussion

  1. I use this version to show latest git branches with the timestamp as a relative/human readable format:

    git for-each-ref --sort='-authordate:iso8601' --format=' %(authordate:relative)%09%(refname:short)' refs/heads
    • Eoghan

      Hey man, I used your snippet and noticed after a while that it actually gets the timing wrong. For example a branch I created today is showing as being worked on 2 weeks ago

    • Philipp

      That might be because the timestamp doesn’t show the branch creation time but the commit time of the last commit of the branch.

  2. I’m definitely going to bookmark this as I’m using git more and more in team settings where we will probably be branching a lot more. This will come in handy.

  3. Glenn

    Super-helpful even three years on — thank you!

    FWIW, I found it much easier to read by putting the date first, as in Amy’s example. That also allows for reverse sorting if desired.

  4. Fredrik

    This is gold, thanks a lot!

  5. John Dawson

    So helpful! I’ve accumulated dozens of branches in my main project over the years. Sorting them by date, and seeing the dates, is super helpful when trying to purge the useless ones while still keeping around some of the just-in-case guys.

  6. Nikita

    Good feature, thanks a lot!

    Also I add that you can add this command to .gitconfig e.g. like a “lastbrs” (last branches) and add the flag “–count” to prevent display all the branches.

  7. Viktor

    You can also just do git branch --sort=-committerdate to get the local branches with the most recently committed to on top.

  8. Viktor

    Also, forgot to mention. If you want this to be the default sort order for all your repositories. Just do git config --global branch.sort -committerdate.

    After that a regular call to git branch will sort like this.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!