Recovering with reflog

Topic 4 of 4 in Undoing changes6 min readBeginnerFree course

Overview

You’ve learned several commands that move branches around: reset, amend, and soon rebase. Each leaves commits behind that no branch points to. They look gone — git log doesn’t show them — but Git keeps a diary of every place HEAD has been. That diary is the reflog, and it makes almost every Git mistake recoverable.

git reflog: the safety net. After a reset –hard, main points at 7d and commit c9 looks lost — no branch points to it, but the reflog remembers every place HEAD has been. git reflog lists 7d02b4e HEAD@{0}: reset: moving to HEAD~1, c91e5a0 HEAD@{1}: commit: Add pricing, and 7d02b4e HEAD@{2}: commit: Style header. Recover with git reset –hard c91e5a0, or keep it on a new branch with git branch rescue c91e5a0. Reflog entries are local only and expire — by default after 30 days for commits no branch points to.

Reading the reflog

Terminal
git reflog
Text
3a1b2c4 HEAD@{0}: reset: moving to HEAD~2
f6e5d4c HEAD@{1}: checkout: moving from dark-mode to main
9b8a7c6 HEAD@{2}: commit: Add dark theme
f6e5d4c HEAD@{3}: checkout: moving from main to dark-mode
f6e5d4c HEAD@{4}: commit: Add search tests
e1d2c3b HEAD@{5}: commit: Add search
3a1b2c4 HEAD@{6}: commit (initial): Start app

Each line is a moment when HEAD moved, newest first:

  • the commit HEAD pointed to afterwards,
  • a name for that moment: HEAD@{0} is now, HEAD@{1} the moment before, and so on,
  • what happened: commit, checkout, reset, merge, rebase, amend…

Every HEAD@{n} works anywhere Git expects a commit: git show HEAD@{4}, git diff HEAD@{1} HEAD, git switch --detach HEAD@{2}.

Recovering after a bad reset

You ran git reset --hard HEAD~2 and lost two commits. In the reflog above, HEAD@{1} is where you were just before the reset. Go back there:

Terminal
git reset --hard HEAD@{1}

Your branch is exactly as it was before the reset. (Be careful to pick the right entry — read the descriptions, and git show HEAD@{1} first if unsure.)

Recovering a deleted branch

When you delete an unmerged branch, Git prints its last commit — and warns you:

Text
Deleted branch dark-mode (was 9b8a7c6).

If you still have that ID, git branch dark-mode 9b8a7c6 recreates it. If not, find the branch’s last commit in the reflog (commit: Add dark theme) and do the same with that ID.

Recovering from amend and rebase

The same works for any command that replaces commits:

  • After git commit --amend, the old version of the commit is at HEAD@{1}.
  • After a rebase that went wrong, find the entry just before rebase (start) and reset to it. Git also saves the pre-rebase position as ORIG_HEAD, so git reset --hard ORIG_HEAD often undoes a rebase or merge immediately afterwards.

The safe way: rescue to a branch

If you’re unsure, don’t reset — create a branch at the old commit and inspect it:

Terminal
git branch rescue HEAD@{4}
git log --oneline rescue

Nothing on your current branch changes, and the rescued commits are safe because a branch now points to them. Merge, cherry-pick or reset once you’re sure.

Branch reflogs

Each branch has its own reflog too:

Terminal
git reflog show main          # every position main has had
git log -g main               # the same, in log format

Useful time-based names: main@{yesterday}, main@{2.hours.ago} — where main was at that time.

Limits of the reflog

  • It’s local. It’s never pushed or cloned. Your colleague’s reflog can’t help you, and a fresh clone has an empty one.
  • It expires. By default, entries for commits that are no longer on any branch expire after 30 days, and other entries after 90 days. After that, git gc may delete those commits for good.
  • It only knows about commits. Edits you never committed or stashed can’t be recovered — the reflog has nothing to point to. (If you lose staged-but-uncommitted changes, git fsck --lost-found can sometimes find the blobs, but don’t count on it.)

The lesson behind the lesson: commit early and often, even on a scratch branch. Once something is committed, the reflog makes it very hard to lose.

Exercise

Rescue lost work

Two disasters happen in the script. First, someone runs git reset --hard HEAD~2, losing the commits “Add search” and “Add search tests”. Then they delete a branch called dark-mode that was never merged. Use git reflog to find both, restore main so it includes the search commits again, and recreate the dark-mode branch at its old tip.

Terminal
mkdir rescue && cd rescue && git init -b main
echo "app" > app.txt && git add . && git commit -qm "Start app"
echo "search" > search.txt && git add . && git commit -qm "Add search"
echo "tests" > search.test.txt && git add . && git commit -qm "Add search tests"
git switch -qc dark-mode
echo "dark" > theme.txt && git add . && git commit -qm "Add dark theme"
git switch -q main

git reset --hard HEAD~2          # disaster 1
git branch -D dark-mode          # disaster 2

# 1. Look at the reflog of HEAD

# 2. Put main back where it was before the reset

# 3. Recreate dark-mode

Summary

  • The reflog records every position HEAD (and each branch) has had: commits, resets, checkouts, rebases, amends.
  • git reflog lists them newest first as HEAD@{n}; any entry can be used like a commit name.
  • Recover with git reset --hard HEAD@{n} or keep the old state on a new branch: git branch rescue <id>.
  • The reflog is local only and expires — by default after 30 days for unreachable commits, 90 for reachable ones.
  • Reflog can’t recover changes that were never committed or stashed.