Sort branches by last commit date

6 days ago (ryangreenberg.com)

I have been using the `git lb` alias suggested by https://ses4j.github.io/2020/04/01/git-alias-recent-branches... (post written April 1st but not a joke) and quite enjoying it. Shows me the last 10 branches I worked on (very easy to edit the alias definition to get more or fewer than 10), sorted by date, most recent first. It's been handy in all sorts of situations, especially because one of our internal libraries needs a bugfix or a new feature every 3 months or so. So every time I cd into that repo, I have no clue what I did last, but `git lb` tells me right away.

Even better (IMHO!) is `git config branch.sort -committerdate` (note the `-`). This will sort newest committer date first.

  • Even better, "hey, chat GPT, list my branches by commit date."

    I know that's not a popular answer, but I'm just trying to demonstrate the reality that this kind of knowledge isn't specialized anymore.

    • Heh. "Just Google it" answers we're never particular conversation lubricant as they are not now. But of course you are right we live in age of wonder once again.

FYI/fwiw Fish shell autocompletes branches sorted by last commit date when writing out git commands. It also provides completions for commitish objects grouped by class, giving precedence to the usual targets first, with each group sorted by its own sort key. Handy!

Our team's `git branch` output is a graveyard. Sorting by last commit would be a godsend for quickly identifying active work and stale branches.

I have been using something like this for 10+ years by this point - still wonder why it is not default or easier to do than a custom gitconfig/alias hack. :P

  • Probably because most of the time you list branches you goal isn't to discover what exists, but to find the specific one you're looking for.

  • > Just configure git branch to sort by the last commit date:

    > git config branch.sort committerdate

    > You can also supply this on a one-off basis as git branch --sort committerdate.

    These don't seem like hacks to me. What do you have in mind?

Such a small quality of life improvement. My `git branch` output is a graveyard sometimes, this would be handy.

How many branches would you need to have to make this worthwhile?

  • At any time I'm working across 5-30 branches (some when I'm reviewing other folks' code) so it's handy to see my latest branches

  • At my current job it's fairly frequent that I have 5+ active branches open for a repo. Not ideal and that's another topic, but it is nice to be able to sort branches because of that.

  • I've seen monorepos with over 10k active branches. But even at a smaller scale (10-100 branches), it becomes useful when manually stacking and/or rebasing, especially if you have a smaller terminal.

yep, i have a little helper in my zshrc for this sort of thing:

alias git-hot-refs="git branch --sort=committerdate | tail"

Let's sort git branches by quality, better branches towards the top, slop at the bottom.

Another useful one if you have a large number of tags and want to see the most recently created ones first:

    git config --global tag.sort -creatordate

(`-creatordate` reverses the sort so that the newest come first.)

Feels like the type of problem you could simply choose to not have in the first place (by not using git).