Sort git Branches by Date
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!
One event that's always been lacking within the document is a signal for when the user is looking at a given tab, or another tab. When does the user switch off our site to look at something else? When do they come back?
With Firefox OS, asm.js, and the push for browser performance improvements, canvas and WebGL technologies are opening a world of possibilities. I featured 9 Mind-Blowing Canvas Demos and then took it up a level with 9 Mind-Blowing WebGL Demos, but I want to outdo...
I'm a big fan of video games. I don't get much time to play them but I'll put down the MacBook Pro long enough to get a few games in. One of my favorites is Portal. For those who don't know, what's...
I know I've harped on this over and over again but it's important to enhance pages for print. You can do some things using simple CSS but today's post features MooTools and jQuery. We'll be taking the options of a SELECT element and generating...
I use this version to show latest git branches with the timestamp as a relative/human readable format:
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
That might be because the timestamp doesn’t show the branch creation time but the commit time of the last commit of the branch.
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.
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.
This is gold, thanks a lot!
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.
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.
You can also just do
git branch --sort=-committerdate
to get the local branches with the most recently committed to on top.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.