July 6, 2021
4 min read
Sometimes when I come back to work after the weekend I forget what I was working on the previous week. This gets even worse when I'm working on multiple tasks at once. The good news is that since most of my work accompanies a branch in git I can use some commands to see what I was working on most recently.
The git branch
command will list all of the branches in the current repository. By default, this list of branches is sorted alphabetically. To change this order, we can use the --sort
flag. There are a lot of different fields we can use to sort. To list branches chronologically by most recent commit we're only interested in the committerdate
field.
git branch --sort=-committerdate
We used a -
before the committerdate
sort field in the command above. This is because we want to sort in reverse chronological order (most recent comes first).
Sort lets you sort in both ascending and descending order with this -
character omission or addition. We only care about descending order for this post though!
git branch --sort=-committerdate # DESCENDINGgit branch --sort=committerdate # ASCENDING
Most often when I run git branch
I would rather see my branches sorted by most recent commit date than alphabetically. Thankfully, the branch
command lets us set our default sorting order via a git config variable called branch.sort
.
git config --global branch.sort -committerdate
I chose to set this sort default using --global
because I want this behavior in all repositories on my computer, not just one. With the above variable set, I can now just run git branch
to see my most recently edited branch.
In case you decide you no longer like your branches sorted in reverse chronological order, you can unset the global variable branch.sort
.
git config --global --unset branch.sort