Stop writing scripts to sort git branches: use this one-liner
Sort branches by last commit date
A developer shares a simple git config trick that eliminates the need for a custom script to sort branches by last commit date. The command `git config branch.sort committerdate` makes `git branch` list branches in order of their most recent commit, and the same can be done on a one-off basis with `git branch --sort committerdate`. The post also highlights other useful sort keys from `git-for-each-ref`, such as `authordate`, `contents:size`, and `upstream`.
Turns out with modern git it’s totally unnecessary.
- rmunn
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.
- asb
Even better (IMHO!) is `git config branch.sort -committerdate` (note the `-`). This will sort newest committer date first.
- ComputerGuru
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!
- jbranchaud
The best upgrade to these kinds of git branch scripts is piping them to fzf. I’ve been using this fbr one from the fzf repo. https://github.com/jbranchaud/dotfiles/blob/main/zshrc.local...
- metmac
Huge fan of `git recent` [0] for this purpose.