Create an Alias to List Git Branches by Date


Sometimes I wouldn't realize how many random one-off branches I had saved locally until one day, after running the generic git branch command, that I had no idea which branch was actually the one I needed. I wanted to be able to quickly see if a branch:

  1. Contained the right work
  2. Was the most recent one (I was very sloppy about naming things similarly over a long period of time. Don't be like me.)

So instead of checkout out each branch one by one and looking at the commit history, and trying to read the not-so-nicely-formatted dates, I found what is now one of my favorite commands ever.

It is possible to list your local git branches ordered by most recent commit date, while showing some extra information, like the abbreviated commit sha and commit message.

N.B. The following instructions are for Mac OSX command line users - be sure to add newlines where used below!. I just wanted to make the alias readable.

git for-each-ref --sort=-committerdate refs/heads/ --color --format=
%(committerdate:format:"%m/%d/%Y\ %H:%M");
%(color:yellow)%(objectname:short)%(color:reset) %(color:italic)%
(contents:subject)%(color:reset);" | column -t -s ";"

Your results should look something like this:

list of git branches

If you're human, you'll never remember that whole command. I personally have it saved as an alias called gbl (like "git branch list") that you can create as well by following along below.

To make aliases permanent, we have to set them in your bash profile file that gets read when you open Terminal (or whatever client you use - I personally use iTerm). They don't all share the same name, but some common ones are ~/.bashrc and ~/.bash_profile. If you're having trouble finding yours, it should be located in your home directory. If not, you can create one yourself!

For this example, let’s use ~/.bash_profile.

On a new line in your profile file, lets create an alias called gbl for that command that lists out our branches by updated date:

N.B. Be sure to add newlines where used below!. I just wanted to make the alias readable.

alias gbl='git for-each-ref --sort=-committerdate refs/heads/
--color --format="%(HEAD)%(color:bold)%(refname:short)%(color:reset);
%(committerdate:format:"%m/%d/%Y\ %H:%M");
%(color:yellow)%(objectname:short)%(color:reset) %(color:italic)%
(contents:subject)%(color:reset);" | column -t -s ";"'

Great! Save the file and head back to your terminal app.

Now, we have to tell our terminal application to reload and get the latest changes to the bash profile file. If yours was located at ~/.bash_profile, you would run the following command in your terminal:

source ~/.bash_profile

If you try running gbl in your command line, you should see your branches listed out, sorted by the date of the last commit to the branch!