Git Foundations / Recover and Diagnose

Recover work with the reflog

Lesson 29 of 33. Open to read. No account needed.

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 restore or git reset --hard before 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.reflogExpire and gc.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.

  1. 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
    
  2. Everything seems gone:

    git log --oneline --all
    

    Only "Start the poem collection" is listed. No branch points at the haiku commits or the two poems any more.

  3. Read the reflog:

    git reflog
    

    The newest line is the reset: moving to HEAD~2. Below it are commit: Add winter poem, commit: Add autumn poem, the switch from haiku to main, and the two haiku commits.

  4. Undo the reset. Copy the ID from the commit: Add winter poem line, then:

    git reset --hard <ID>
    git log --oneline
    

    main has its three commits back, ending with "Add winter poem".

  5. Recreate the deleted branch. Copy the ID from the commit: Add second haiku line line, then:

    git branch haiku <ID>
    git log --oneline haiku
    git log --oneline --all --graph
    

    haiku exists 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 seeLikely causeNext step
The reflog has no line for a commitIt was never committed, or this is a different cloneRecovery is only possible in the clone where the commit was made
HEAD@{n} led to the wrong commitThe numbers shifted after another commandCopy the commit ID instead
fatal: a branch named 'haiku' already existsThe branch was recovered already, or never deletedCheck with git log --oneline haiku
fatal: not a valid object nameA mistyped IDCopy it again from git reflog
  1. 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

  1. You discarded an edit with git restore poems.md an hour ago. Can the reflog bring it back? Why or why not?
  2. 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

  1. No. The reflog records where HEAD and branches pointed, which are always commits. The discarded edit was never committed, so no entry refers to it.
  2. 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.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy