What is git reflog used for?
Learn what git reflog tracks, how it recovers commits lost after a hard reset or bad rebase, and why it's a local-only safety net distinct from git log.
Expected Interview Answer
git reflog is a local log of every place HEAD and branch pointers have been in your repository, letting you recover commits that seem lost after a reset, rebase, checkout, or branch deletion, even if no branch or tag currently references them.
Every time HEAD moves — a commit, checkout, merge, rebase, or reset — Git records an entry in the reflog with a timestamp and a short description. Because these entries reference the actual commit objects, you can check out or reset to any of them directly, effectively undoing operations that look destructive. The reflog is purely local (never pushed or shared) and entries expire after roughly 90 days by default, so it is a safety net, not a permanent history mechanism like git log.
- Recovers commits lost after a hard reset or bad rebase
- Restores a branch that was deleted by mistake
- Shows every position HEAD has ever held locally
- Provides a safety net distinct from permanent commit history
- Essential for confidently undoing risky Git operations
AI Mentor Explanation
git reflog is like the twelfth man's private notebook tracking every single change of batting order and bowling change made during a match, even the ones later scrubbed from the official scorecard. If the team wrongly believes a good batting order was lost, that notebook shows where things stood, letting the captain restore it.
Step-by-Step Explanation
Step 1
HEAD moves are logged
Every commit, checkout, merge, rebase, or reset appends a timestamped entry to the local reflog.
Step 2
View the reflog
Run git reflog (or git reflog show HEAD) to list recent HEAD positions with short descriptions like commit or rebase.
Step 3
Find the lost commit
Scan the entries for the operation just before things went wrong, such as HEAD@{2} before a hard reset.
Step 4
Recover it
Run git checkout HEAD@{2} or git reset --hard HEAD@{2} to return to that exact prior state.
Step 5
Know the limits
Reflog entries are local-only, are not pushed with the repo, and expire (default ~90 days for reachable entries, ~30 for unreachable).
What Interviewer Expects
- Explains reflog tracks every local movement of HEAD
- Knows it can recover commits after reset --hard or a bad rebase
- Understands reflog is local-only and never pushed or shared
- Mentions the default expiration windows for reflog entries
- Can demonstrate recovering a commit with git reset --hard HEAD@{n}
Common Mistakes
- Assuming a hard reset permanently destroys commits with no recovery
- Confusing git reflog with git log (log is the permanent, shared history)
- Thinking reflog entries are pushed to the remote
- Not knowing reflog entries eventually expire and get garbage collected
Best Answer (HR Friendly)
“git reflog is a personal safety net that records every place your work has been locally, even changes you think you've lost after a mistake like an accidental reset. It lets you jump back to an earlier state that isn't visible in the normal project history, but only on your own machine — it's never shared with teammates.”
Code Example
git reset --hard HEAD~3 # oops, lost 3 commits' worth of work
git reflog
# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
# e4f5g6h HEAD@{1}: commit: add feature X
git reset --hard HEAD@{1} # restores the commit before the mistaken resetFollow-up Questions
- How long do reflog entries persist before expiring by default?
- Why is git reflog local-only and never pushed to a remote?
- How would you recover a branch that was deleted by mistake?
- What is the difference between git reflog and git log?
- How does the reflog help after an interrupted or botched rebase?
MCQ Practice
1. What does git reflog track?
reflog logs every local position HEAD has held, including commits, checkouts, resets, and rebases, with timestamps.
2. Is the reflog shared with other collaborators via push?
The reflog lives only in your local .git directory and is never transmitted during push, fetch, or clone.
3. How would you recover commits lost after git reset --hard?
git reflog shows the HEAD position just before the reset, letting you git reset --hard back to that commit.
Flash Cards
What does git reflog record? — Every local movement of HEAD — commits, checkouts, resets, rebases — with a timestamp and description.
Is the reflog shared with remotes? — No, it's purely local and never pushed, fetched, or cloned.
How do you recover a commit lost after a hard reset? — Find the prior entry in git reflog and run git reset --hard HEAD@{n} to that entry.
Do reflog entries last forever? — No, they expire by default (around 90 days for reachable, 30 for unreachable) before garbage collection.