List Recent Git Branches
September 23, 2021
If you are anything like me, you have a lot of branches in your local projects and you'd like to quickly find a list of the most recently updated branches so you can keep on working. The following are a few ways that you might find handy.
Git Command
Although you might be used to running git branch
by itself, there are actually
a lot of options that you can pass to thegit
branch
command! For our purposes, there
is the --sort
option and you can provide committerdate
as the value!
git branch --sort=-committerdate
Git Config Alias
If you'd rather not remember the --sort=-committerdate
option, you could
create a .gitconfig
alias. The following snippet adds a recent
alias so that
you can run git recent
from the command line.
[alias]
recent="branch --sort=-committerdate"
Zsh Shell
Or you could go another route and combine this command along with fzf
to
create a much more interactive experience.
The following creates a cb
alias in my .zshrc
file that will list out my
branches (sorted by commit date) and then pipe those into
fzf
so that I can choose which branch I'd
like to checkout.
alias cb='git branch --sort=-committerdate | fzf --header Checkout | xargs git checkout'
NOTE: You will need to install
fzf
for this alias to work. You can find installation instructions from their README.md.
Tweet about this post and have it show up here!