Git Reflog

Git reflog is a command that could save your bacon and has the power to unbork you out of a bad situation.

Published on February 21, 2021

Git reflog is one of those commands that sounds weird and makes me wonder what kind of strange person would ever need to use it. It sounds weird to me, because I've always pronounced it as re-flog. I didn't realize it's short for reference logs. Once you actually know what it does, it's kind of cool, like a time machine, for when you totally bork something and want to go back to a time just before you borked something. I've only used this a couple of times after I found out what it actually does, but I would have used it more when I was learning about the nuances of Git.

Git reflog

First I would like to show you how cool reflog is by running it. (I keep pronouncing it re-flog in my head and I can't stop.)

$ git reflog

git reflog results

When I read the results from top to bottom, I can remind myself of how indecisive I am and see how I'm switching back and forth between branches.

git reflog branch switching

I'm going to push a commit from my git squash tutorial

git reflog no difference

I can see that it made no difference. That's because git reflog only cares about when tips of branches and other references are updated locally, which is why it logged instances of me switching between branches.

All this is real swell so far, but what about a (somewhat) practical example.

Use case

In this example, I branched off of master and I added 3 commits locally.

git reflog updates

Let's say I wanted to get rid of the last commit 433efb7, and did this by running the following command:

$ git reset --hard HEAD~2

git reflog reset too far

Oops, I went too far. I only have the 4th update commit, when I also wanted to have the 5th one.

git reflog only fourth update

Rather than restart my work, I can use git reflog to unbork this situation. I know that running git log doesn't show my removed commits, but git reflog will.

$ git reflog

git reflog found previous commit

I can see that git reflog contains the commit hash (d224a3d) where I borked things, which is highlighted in the yellow box. I can also see the commit hash ( 98b366f) of where I actually wanted to be before I borked everything, highlighted in the green box. Since I haven't made any other recent changes, I can just go to the commit that I wanted to be at in the beginning. I can do this by doing another hard reset to the 98b366f - 5th update commit.

$ git reset --hard 98b366f

git reflog reset to previous commit

git reflog where I want to be

Now I'm where I wanted to be in the first place and nobody knows that I almost completely removed my previous work by mistake. If I look at the reflogs again, I can see that this recent change will also be recorded.

$ git reflog

git reflog bork unbork history

This is why git reflog is like a time machine and can be a life saver or at least an anxiety reducer when making mistakes in rebasing, merging, and all that fun stuff that git users get to have.

References

  1. git reflog