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.
Although you might be used to running
git branch by itself, there are actually
a lot of options that you can pass to the
branch command! For our purposes, there
--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
.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"
Or you could go another route and combine this command along with
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
fzffor this alias to work. You can find installation instructions from their README.md.
Tweet about this post and have it show up here!