Recovering with reflog
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.
Reading the reflog
git reflog3a1b2c4 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 appEach line is a moment when HEAD moved, newest first:
- the commit
HEADpointed 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:
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:
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 atHEAD@{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 asORIG_HEAD, sogit reset --hard ORIG_HEADoften 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:
git branch rescue HEAD@{4}
git log --oneline rescueNothing 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:
git reflog show main # every position main has had
git log -g main # the same, in log formatUseful 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 gcmay 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-foundcan 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.
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.
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-modemkdir 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
git reflog
# … HEAD@{0}: reset: moving to HEAD~2
# … HEAD@{1}: checkout: moving from dark-mode to main
# … HEAD@{2}: commit: Add dark theme
# … HEAD@{3}: checkout: moving from main to dark-mode
# … HEAD@{4}: commit: Add search tests
# 2. Put main back where it was before the reset
git reset --hard "HEAD@{1}"
git log --oneline # Add search tests / Add search / Start app
# 3. Recreate dark-mode from the commit in the reflog
tip=$(git reflog --format="%h %gs" | grep "commit: Add dark theme" | head -1 | cut -d" " -f1)
git branch dark-mode "$tip"
git log --oneline dark-mode # Add dark theme / Add search tests / …Summary
- The reflog records every position
HEAD(and each branch) has had: commits, resets, checkouts, rebases, amends. git refloglists them newest first asHEAD@{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.