2 min read

Tags

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:

  • git reflog: Without parameters like this one it, reflog will default to show
  • egrep -io "moving from ([^[:space:]]+)": Looks for the phrase “moving from “ which is common to any entries for checkout commands (we can also just look for “checkout:” but I like living on the edge)
  • awk '{ print NR " - " $3 }': Prints out the line number (NR) and the third parameter in the line (the “from” branch name) separated by a dash
  • head -n 5: limits the output to five lines

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: