Commits Are Hard to Lose
Earlier lessons promised that commits you reset away, rebase or delete a branch
for are kept for a while. This lesson shows where to find them. Whenever
HEAD or a branch moves, Git writes a line in the reflog, the reference
log. Even when no branch points at a commit any more, the reflog still
remembers it, so you can give it a name again.
Reading the Reflog
git reflog
Illustrative output, newest first:
fb2ccb7 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~2
fc3868f HEAD@{1}: commit: Add winter poem
863054e HEAD@{2}: commit: Add autumn poem
fb2ccb7 HEAD@{3}: checkout: moving from haiku to main
a1662fe HEAD@{4}: commit: Add second haiku line
Each line gives the commit HEAD pointed at after a move, a name for that
position, and what caused the move. HEAD@{1} means "where HEAD was one move
ago". Those numbers shift every time HEAD moves, so when recovering, copy the
commit ID from the start of the line instead.
The reflog includes commits, resets, switches between branches, merges,
rebases and amends, which is why it can find commits from a deleted branch: you
made them while HEAD was on that branch.
Recovering
Once you have the ID of the commit you want:
git branch <new-name> <ID> # give lost commits a branch again
git reset --hard <ID> # move the current branch back to them
Creating a branch is the safest choice: it changes nothing else, and you can
inspect the result before deciding what to do with it. Moving the current
branch with reset --hard suits undoing a mistaken reset, provided git status
shows nothing uncommitted.
What the Reflog Cannot Do
- It only knows about commits. Edits you discarded with
git restoreorgit reset --hardbefore committing were never recorded, so the reflog has nothing to offer. - It belongs to one clone. The reflog is not pushed or cloned. Your teammate's clone has its own, and the shared repository's history does not include yours.
- Entries expire. By default, entries are kept for 90 days, and entries
for commits no longer on any branch for 30 days. The settings are
gc.reflogExpireandgc.reflogExpireUnreachable. After that, Git's routine housekeeping,git gc, which runs automatically from time to time, can delete commits nothing refers to.
So recover soon, and commit work you care about: a commit is far easier to get back than an uncommitted edit.
Exercise: Recover a Deleted Branch and a Reset Commit
The setup makes two mistakes on purpose: it deletes an unmerged branch with
-D, then resets main back two commits with --hard. You will recover both.
Starting state: Git configured as in the configuration lesson.
-
Run the setup, mistakes included:
mkdir -p ~/git-practice/reflog-recovery cd ~/git-practice/reflog-recovery git init -b main cat > poems.md <<'EOF' # Poems EOF git add poems.md git commit -m "Start the poem collection" git switch -c haiku cat > haiku.md <<'EOF' Morning rain falls soft EOF git add haiku.md git commit -m "Add first haiku line" cat >> haiku.md <<'EOF' on the quiet garden path EOF git commit -am "Add second haiku line" git switch main git branch -D haiku cat >> poems.md <<'EOF' Autumn: leaves drift down. EOF git commit -am "Add autumn poem" cat >> poems.md <<'EOF' Winter: snow on the gate. EOF git commit -am "Add winter poem" git reset --hard HEAD~2 -
Everything seems gone:
git log --oneline --allOnly "Start the poem collection" is listed. No branch points at the haiku commits or the two poems any more.
-
Read the reflog:
git reflogThe newest line is the
reset: moving to HEAD~2. Below it arecommit: Add winter poem,commit: Add autumn poem, the switch fromhaikutomain, and the two haiku commits. -
Undo the reset. Copy the ID from the
commit: Add winter poemline, then:git reset --hard <ID> git log --onelinemainhas its three commits back, ending with "Add winter poem". -
Recreate the deleted branch. Copy the ID from the
commit: Add second haiku lineline, then:git branch haiku <ID> git log --oneline haiku git log --oneline --all --graphhaikuexists again with both haiku commits on top of the first commit.
Success: "Add winter poem" is back on main, and both haiku commits are on
the recreated haiku branch.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
| The reflog has no line for a commit | It was never committed, or this is a different clone | Recovery is only possible in the clone where the commit was made |
HEAD@{n} led to the wrong commit | The numbers shifted after another command | Copy the commit ID instead |
fatal: a branch named 'haiku' already exists | The branch was recovered already, or never deleted | Check with git log --oneline haiku |
fatal: not a valid object name | A mistyped ID | Copy it again from git reflog |
-
Clean up:
cd ~/git-practice ls rm -rf reflog-recovery ls
Independent task: before cleaning up, make a commit and then amend it:
cat >> poems.md <<'EOF'
Spring: the river wakes.
EOF
git commit -am "Add spring poem"
cat >> poems.md <<'EOF'
Summer: long light.
EOF
git commit -a --amend -m "Add spring and summer poems"
The amend replaced "Add spring poem". Find the original in the reflog and give
it a branch named before-amend. Confirm that the commit on that branch adds
the spring line but not the summer line.
Check Your Understanding
- You discarded an edit with
git restore poems.mdan hour ago. Can the reflog bring it back? Why or why not? - Ravi deleted an unmerged branch in his clone. Can Asha recover it from her clone's reflog?
Answer and Reasoning
Independent task
git reflog -3
git branch before-amend <ID>
git show before-amend
git reflog -3 shows the three newest entries. The newest is commit (amend): Add spring and summer poems; the line commit: Add spring poem holds the
original's ID. After creating the branch, git show before-amend shows that
commit: its changes add Spring: the river wakes. and nothing about summer.
Understanding check
- No. The reflog records where
HEADand branches pointed, which are always commits. The discarded edit was never committed, so no entry refers to it. - No. Each clone keeps its own reflog, and reflogs are not shared. Only Ravi's clone can recover the branch, and only while its entries have not expired.