Course contentsShow
← Computer Science
Lesson 2207 of 2,87249. Version Control with GitFree lesson

Reflog and Recovery

Use git reflog to recover lost commits, undo rebases, and restore deleted branches.

Reflog and Recovery

What you'll learn: How to use Git's reflog to resurrect lost commits, undo complex operations, and recover work you thought was gone forever.

What is the Reflog?

Git's reflog (reference log) is your safety net—a local history of where your HEAD and branch references have pointed over time. Think of it as Git's "undo history" that records every move you make, even ones that disappear from normal commit history.

When you accidentally delete a branch, reset too hard, or botch a rebase, the commits aren't truly gone—they're just orphaned. The reflog remembers where they are.

Using git reflog

Run git reflog to see a chronological list of recent HEAD movements:

a1b2c3d HEAD@{0}: commit: Add feature
e4f5g6h HEAD@{1}: rebase: checkout main
i7j8k9l HEAD@{2}: commit: WIP changes

Each entry shows a commit hash and what operation moved HEAD there. The HEAD@{n} notation lets you reference these positions.

Recovery Scenarios

Lost commits after reset: If you ran git reset --hard and lost work, find the commit in the reflog and restore it:

git reset --hard HEAD@{2}

Deleted branch: The commits still exist. Find the branch's last position in the reflog and recreate it:

git branch recovered-branch a1b2c3d

Undoing a rebase: Find the pre-rebase HEAD position and reset to it.

The reflog typically keeps entries for 90 days (30 for unreachable commits), giving you a generous window for recovery.

Key Takeaway: The reflog is Git's black box recorder—use git reflog to find and recover seemingly lost commits, giving you confidence to experiment without fear of permanent data loss.