List Recent Git Branches

Published

September 23, 2021

Reading time
2 min read
This post is part of my Byte Series, where I document tips, tricks, and tools that I've found useful.

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.

~/.gitconfig
[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.

~/.zshrc
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.

Web Mentions
0
0

Tweet about this post and have it show up here!