I want to get a list of all the branches in a Git repository with the "freshest" branches at the top, where the "freshest" branch is the one that's been committed to most recently (and is, therefore, more likely to be one I want to pay attention to).
Is there a way I can use Git to either (a) sort the list of branches by latest commit, or (b) get a list of branches together with each one's last-commit date, in some kind of machine-readable format?
Worst case, I could always run git branch
to get a list of all the branches, parse its output, and then git log -n 1 branchname --format=format:%ci
for each one, to get each branch's commit date. But this will run on a Windows box, where spinning up a new process is relatively expensive, so launching the Git executable once per branch could get slow if there are a lot of branches. Is there a way to do all this with a single command?
I was able to reference the examples above to create something that works best for me.
I also needed colors, tags and remote references without any duplicates:
Because quoting can be hard, here the alias for bash:
Here's the variation I was looking for:
That
tail -r
reverses the list so the most-recentcommiterdate
is last.Here's the optimal code, which combines the other two answers:
Based on ilius' version, but with the current branch shown with a star and in color, and only showing anything that is not described as "months" or "years" ago:
I came up with the following command (for Git 2.13 and later):
If you don’t have
column
you can replace the last line withThe output looks like
I wrote a blog post about how the various pieces work.