Git Reflog: Recover Lost Commits

Summary: Use git reflog to recover from accidental mistakes.


Accidentally deleted a branch? Force-pushed over some important work? If you've ever made a mistake in Git that left you fearing your commits are lost forever, take a deep breath. There's a hidden superhero in Git called reflog that just might save your day. In this post, we’ll explore what git reflog is, how it works, and exactly how to use it to recover lost commits.


What is Git Reflog?

Most Git users are familiar with the concept of commit history. However, did you know that Git keeps its own secret log of everything you do with branches and commits? This internal record is called the reflog.

Reflog stands for reference log. Every time you move the tip of a branch—by committing, rebasing, resetting, merging, or cherry-picking—Git captures the previous position in the reflog. This log is stored locally and is unique to your repository.

The reflog is not part of your public repository. Other collaborators won’t see your reflog entries, and if you delete your clone, your reflog goes with it.


Why is Reflog Important?

Sometimes, you'll:

  • Commit work, then accidentally delete a branch.
  • Reset or rebase to an older state, losing newer commits.
  • Force-push and overwrite local/fetched work.

These actions seem destructive, but so long as the garbage collector hasn't run and permanently deleted unreferenced commits, you can use the reflog to recover almost any lost commit.


Viewing the Reflog

To open the reflog for your current repository, use:

git reflog

You'll see output like:

68f7d39 (HEAD -> main) HEAD@{0}: commit: Add final touches
b5f8a12 HEAD@{1}: reset: moving to b5f8a12
aa12bc9 HEAD@{2}: commit: Implement new feature
d234fef HEAD@{3}: commit: Update README
...
  • The numbers in brackets (e.g., HEAD@{2}) denote the position in the reflog: {0} is most recent.
  • Each entry shows the commit, reference movement, and action.

Recovering Lost Branches or Commits

Let’s say you accidentally deleted a branch after some important commits. Here’s how to restore it:

1. Check the Reflog

Suppose you lost a branch named feature-x. Run:

git reflog

Find the commit hash for the point just before you deleted or reset the branch. For example:

b8e2ff5 HEAD@{5}: checkout: moving from feature-x to main
...
8c371e9 HEAD@{8}: commit: Implement core of feature-x

2. Recover the Lost Commit

To restore the commit, you can create a new branch pointing to the lost commit:

git checkout -b feature-x <commit-hash>

So, if 8c371e9 is your commit:

git checkout -b feature-x 8c371e9

Now feature-x is back, along with your "lost" commit and any descendants.


Undoing a Hard Reset

Suppose you ran a git reset --hard and lost unpushed commits:

  1. Run git reflog to find the commit hash before the reset.

  2. You’ll see a line like:

    4f5a482 HEAD@{2}: commit: Wonderful work lost!
    
  3. Reset back to that commit:

    git reset --hard 4f5a482
    

Voila—your commit is restored!


Good to Know

  • Reflog entries are time-limited: By default, they're expired after 90 days (configurable).
  • Reflog is local: It won’t help you recover rails on the remote, only what's in your clone.
  • Don't panic over lost work: As long as the data is still in the reflog, you can retrieve it.

Pro Tips

  • To see another branch's reflog: git reflog show <branch>.
  • To prune expired entries and force cleanup: git reflog expire --expire=now --all && git gc --prune=now --aggressive.
  • Combine with git cherry-pick to recover just certain lost commits.

Conclusion

git reflog is one of Git’s most powerful (and underappreciated) recovery tools. Whenever disaster strikes, remember to inspect the reflog before declaring your work lost. With a little know-how and a careful look through your recent reference history, many “irreversible” mistakes become totally reversible!


Keep calm and reflog on!