Harnessing the Power of Reflogs 🧙‍♀️

10 Aug 2021

A few weeks ago, I tweeted about a discovery that blew my mind:

In the tweet, I referenced the git docs which says:

You can use the @{-N} syntax to refer to the N-th last branch/commit checked out using “git checkout” operation. You may also specify - which is synonymous to @{-1}.

That’s cool and all, but what I realised is that most of the time, aside from the last checked out branch, I don’t remember what the third to last or fifth to last branch was. Actually that’s a lie. Sometimes I don’t remember the last checked out branch too. :woman_facepalming:

All I remember is that I was in that branch but I don’t remember the name nor what number I should put in the braces. About 98% of the time, I ended up consulting the reflog to figure out this information.

The reflog enumerates anything that happens within git. It shows information about references, any commands that have been run, or where HEAD is pointing to, among others.


A sample reflog output

That’s a lot of information, but what we are looking for are lines similar to this one:

1e8a561 (origin/main, main) HEAD@{10}: checkout: moving from main to task/lint-gradle

From here I can use the branch name and go back to that branch if I want to. Scanning all info spewed by reflog can be overwhelming; sometimes I’m just interested in finding out the branch name for checking things out and not in all the other details.

After some furious Googling and some trial and error, I ended up with this:

git reflog | egrep -io "moving from ([^[:space:]]+)" | awk '{ print NR " - " $3 }' | head -n 5'

Let’s break it down:

Running this command gives back:


Convenience!

The numbers on the left match the number I can use for checking out. For example, if I want to go back to feature/package-rename:

gco @{-4}

Since I’m interested in using the numbers, I don’t care if some branch names appear multiple times in the list. To remove duplicates, add this command after the egrep:

awk ' !seen[$0]++'

:bangbang: Note that if you filter out duplicates the numbers on the left cannot be used for the git checkout @{-N} shorthand :bangbang:


Look, I’m an old woman and I like using the command line for git so please don’t @ me with your fancy GUI suggestions. :older_woman:


References: