Recover Lost Commits with Git Reflog

Learn how to use git reflog to recover commits lost after a hard reset, failed rebase, or accidental branch deletion. Your safety net for git disasters.

Undoing Changes

Detailed Explanation

Recovering Lost Commits with Git Reflog

git reflog is your ultimate safety net. It records every change to HEAD, including commits, resets, rebases, and branch switches. Even after a git reset --hard, the reflog can help you recover lost work.

What is the Reflog?

The reflog (reference log) is a local-only log of where HEAD has pointed. Unlike git log which shows commit ancestry, git reflog shows every operation that moved HEAD.

git reflog

Output looks like:

abc1234 HEAD@{0}: reset: moving to HEAD~1
def5678 HEAD@{1}: commit: feat: add user auth
ghi9012 HEAD@{2}: commit: fix: resolve login bug

Recovering After a Hard Reset

# Oops! Accidentally did a hard reset
git reset --hard HEAD~3

# Check reflog for the lost commit
git reflog

# Found it! Restore it:
git reset --hard def5678

# Or create a new branch from the lost commit
git branch recovery def5678

Recovering After a Failed Rebase

# Rebase went wrong
git rebase --abort  # if still in progress

# Or if already completed with bad results
git reflog
# Find the commit hash before the rebase started
git reset --hard HEAD@{5}

Recovering a Deleted Branch

# Accidentally deleted a branch
git branch -D feature/important

# Find the branch tip in reflog
git reflog
# Look for the last commit on that branch

# Recreate the branch
git branch feature/important abc1234

Reflog Expiration

Reflog entries expire after 90 days by default (30 days for unreachable commits). You can change this:

git config gc.reflogExpire 180.days

Important Notes

  • The reflog is local only -- it is not pushed to remotes
  • git gc (garbage collection) can remove old reflog entries
  • Each branch has its own reflog: git reflog show main

Use Case

Git reflog is the last resort for recovering from git disasters. Whether you accidentally ran git reset --hard, botched a rebase, or deleted an important branch, the reflog keeps a record of where HEAD was. It is especially valuable in fast-paced development where mistakes happen. Understanding reflog gives developers confidence to use powerful (and potentially destructive) git commands, knowing they have a safety net.

Try It — Git Command Reference & Cheat Sheet

Open full tool