The Safety Net: Reflog
Last updated on 2026-11-02 | Edit this page
Estimated time: 15 minutes
Overview
Questions
- How do I undo a mistake in git?
- Can I really lose work?
Objectives
- Read the reflog
- Recover from a bad
git reset --hard - Bring back a deleted branch
- Know what the reflog can’t save
Why now?
From the next episode on we will be changing history: amending, resetting and rebasing. If that sounds risky, this episode is the reason it isn’t. Git keeps a private diary of where you’ve been, and it can get you back.
Remember from episode 2: when you “change” a commit, git makes a new one and moves a label. The old commit is still there, but nothing points at it any more. The reflog is how you find it again.
What is the reflog?
Every time HEAD moves (when you commit, switch branch,
reset, rebase, merge…) git writes a line in a log. See yours:
OUTPUT
1e81f27 HEAD@{0}: checkout: moving from experiment to main
fba8092 HEAD@{1}: reset: moving to HEAD@{1}
1e81f27 HEAD@{2}: reset: moving to HEAD~2
fba8092 HEAD@{3}: commit: Note gaussian fitting idea in README
1634522 HEAD@{4}: commit: Note smoothing idea in README
...
(Use q to leave the pager.) Yours will look different,
as it’s a record of your actions.
Each line says: the commit HEAD pointed to,
HEAD@{n} (that position, counting back from now), and what
you did to get there. The newest entry is at the top.
The reflog is:
- local: it lives in your clone and is never pushed. Your colleagues have their own.
- temporary: entries expire. By default after 90 days, or 30 days if the commit is no longer on any branch.
- only about commits. Remember that last point, we’ll come back to it.
Oh no: a bad reset
Let’s make some work and then destroy it. Follow along with the instructor, and type each step yourself. Make a throwaway branch and two commits:
BASH
git switch -c experiment
echo "- Idea: smooth the spectrum before peak finding" >> README.md
git commit -am "Note smoothing idea in README"
echo "- Idea: fit gaussians to the peaks" >> README.md
git commit -am "Note gaussian fitting idea in README"
git log --oneline -3
OUTPUT
fba8092 Note gaussian fitting idea in README
1634522 Note smoothing idea in README
1e81f27 Add plotting of the spectrum and its peaks
Now the mistake. You meant to undo one commit, but you typed a
2 and also added --hard:
OUTPUT
HEAD is now at 1e81f27 Add plotting of the spectrum and its peaks
Both commits seem to be gone, and the lines have vanished from
README.md. (We’ll explain exactly what reset
does in the next episode. For now: it moved the branch label back two
commits and threw away the changes.)
Don’t panic. Look at the reflog. (The hashes in our examples will be different from yours, because they depend on the time you made the commits. Always use the ones on your screen.)
OUTPUT
1e81f27 HEAD@{0}: reset: moving to HEAD~2
fba8092 HEAD@{1}: commit: Note gaussian fitting idea in README
1634522 HEAD@{2}: commit: Note smoothing idea in README
1e81f27 HEAD@{3}: checkout: moving from main to experiment
...
The line
fba8092 HEAD@{1}: commit: Note gaussian fitting idea in README
is where we were before the reset.
flowchart RL
accTitle: {After a reset, the commits still exist and the reflog remembers them}
accDescr: {A chain of three commits, C1, C2 and C3. After the reset the branch experiment points at C1, so C2 and C3 are shown dashed because nothing points at them. The reflog, shown as a dotted note, remembers that HEAD was at C3.}
experiment(["experiment<br/>(after the reset)"]) --> C1
C2 --> C1
C3 --> C2
reflog[/"reflog remembers:<br/>HEAD was at C3"/] -.-> C3
classDef orphan stroke-dasharray: 5 5
class C2,C3 orphan
Dashed commits still exist, but no branch points at them. The reflog is how you find your way back to one.
Move the branch label back there:
OUTPUT
HEAD is now at fba8092 Note gaussian fitting idea in README
Both commits are back, and so is the file’s content.
Use the hash, not HEAD@{n}
You can write git reset --hard HEAD@{1}, and it often
works. But the numbers shift every time HEAD
moves, including when you run the recovery commands. Read the
reflog and copy the hash of the line you want. It never
changes.
A quicker undo: ORIG_HEAD
Before risky commands such as reset, merge
and rebase, git remembers where you were in
ORIG_HEAD. So straight after a mistake,
git reset --hard ORIG_HEAD often puts you back. The reflog
covers the cases where you’ve done other things since.
Oh no: a deleted branch
Delete the branch (we have to switch away first, and -D
forces deletion of unmerged work):
OUTPUT
Deleted branch experiment (was fba8092).
Look: git even tells you the hash. If you missed that, the reflog still has it:
Find the last commit of the branch (the line that says
commit: Note gaussian fitting idea in README) and make a
new label there:
The branch is back, with both commits. Now tidy up:
What the reflog can’t save
The reflog records commits. Work that was never committed isn’t in it:
- Edits you made and never committed, then threw away with
git restore .orgit reset --hard: gone. - Edits you staged but never committed: usually gone too (technically git may keep the content, but finding it is painful).
- Anything older than the expiry time.
- Commits only ever made on someone else’s computer.
The rule that follows: commit before you try something risky. Even a messy “wip” commit, which we know how to tidy up afterwards. A commit can be recovered, and an uncommitted edit cannot.
Knowledge check: can I get it back?
For each situation, can you get the work back? How?
- You committed three times on a branch, then ran
git reset --hard main. - You edited
peaks.py, never committed, and rangit restore peaks.py. - You ran
git branch -D featurebefore it was merged. - You did an interactive rebase last week and now realise you dropped a commit you needed.
-
Yes. Find the last of the three commits in
git reflogandgit reset --hard <hash>. - No. The changes were never committed, so git has no record of them.
-
Yes.
git branch -Dprints the hash of the tip, and the reflog has it too:git branch feature <hash>. -
Yes, as long as it’s within the expiry (90 days /
30 days). The reflog shows where the branch was before the rebase. Look
for the
rebase (start)entry and the one before it. This is why we’ll be relaxed about rebasing.
Pacing. This episode is the follow-along: learners type every step with you, so go slowly and pause after each command until people have caught up. There’s no separate challenge. If someone falls behind, they can watch and catch up at the knowledge check.
Do the “oh no” moments as a live demo with everyone following. The drama is the point: say “I’ve lost all my work!” and pause before looking at the reflog.
Cleanup: make sure everyone ends the episode with main
checked out, and with a clean git status. A bad
README.md change left on main would cause
confusing conflicts later. If anyone committed to main by
mistake: git reset --hard origin/main (they have a fork, so
it will be at the right place).
VS Code has no built-in reflog view. (The GitLens extension adds one.) This is a terminal job.
Common problems:
- The reflog opens in a pager and people don’t know how to leave. It’s
q. - Someone runs
git reset --hardwith uncommitted work, and asks about it. Use it as the live demonstration of “what the reflog can’t save”.
- Git rarely deletes commits straight away. The reflog records where
HEADhas been, so you can find them - Recover by copying the hash from
git reflogand usinggit reset --hard <hash>orgit branch <name> <hash> - The reflog is local and expires, and doesn’t cover uncommitted work
- Commit before trying something risky