What is git reflog and how can it save you from lost commits?
Learn what git reflog is and how it rescues commits lost to reset, rebase, or amend — with commands to find and restore your work safely.
Expected Interview Answer
git reflog is a local log of every position your HEAD and branch references have pointed to, letting you recover commits that seem lost after a reset, rebase, amend, or deleted branch.
Whenever a ref moves — through commit, checkout, reset, rebase, merge, or amend — Git records the previous position in the reflog. Even 'orphaned' commits with no branch pointing at them stay reachable through their reflog entry until garbage collection removes them (typically after 90 days). By finding the SHA in git reflog you can restore that state with git checkout, git reset, or git branch.
- Recovers commits lost after a hard reset
- Restores work discarded during a botched rebase
- Finds commits from a deleted branch
- Undoes an accidental git commit --amend
- Provides a per-repository safety net stored locally
AI Mentor Explanation
git reflog is like a match's ball-by-ball commentary log that keeps every state of the scoreboard even after the operator wrongly resets it. If someone clears the display, the commentary still shows the exact runs and wickets moments earlier, so officials can restore the correct score instead of losing the innings entirely.
Step-by-Step Explanation
Step 1
View the reflog
Run git reflog to see a numbered list of every recent HEAD position with its SHA and the action that moved it.
Step 2
Identify the lost state
Find the entry (e.g. HEAD@{4}) just before the reset, rebase, or amend that discarded your work.
Step 3
Inspect before restoring
Use git show HEAD@{4} or git log HEAD@{4} to confirm it is the state you want back.
Step 4
Recover the work
Run git reset --hard HEAD@{4}, git checkout <sha>, or git branch recovered <sha> to bring it back.
Step 5
Act promptly
Recover before git gc prunes unreachable commits — the default reflog expiry is 90 days (30 for unreachable).
What Interviewer Expects
- Understanding reflog logs every ref movement
- Knowing it is local and per-repository, not pushed
- Recovering after reset --hard, rebase, or amend
- Using HEAD@{n} syntax to reference past states
- Awareness that garbage collection eventually prunes entries
Common Mistakes
- Assuming a hard reset permanently destroys commits
- Thinking reflog is shared via the remote
- Confusing git reflog with git log (log follows history, reflog follows HEAD moves)
- Waiting too long until git gc prunes the orphaned commit
Best Answer (HR Friendly)
“git reflog is like an undo history for your project that quietly records every step you took, so if you accidentally delete or overwrite work, you can look back, find where it was, and bring it back. It is a local safety net that has rescued countless developers from lost commits.”
Code Example
# Oops — a hard reset threw away recent commits
git reset --hard HEAD~3
# Show every position HEAD has pointed to
git reflog
# e.g.:
# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
# f4e5d6c HEAD@{1}: commit: add payment feature
# Inspect the state you want back
git show HEAD@{1}
# Restore it (either reset the branch or create a rescue branch)
git reset --hard HEAD@{1}
# or, non-destructively:
git branch recovered-work f4e5d6cFollow-up Questions
- How does git reflog differ from git log?
- When does git garbage collection remove unreachable commits?
- How would you recover a branch you deleted with git branch -D?
- How can you undo a git commit --amend using the reflog?
MCQ Practice
1. What does git reflog primarily record?
The reflog tracks movements of HEAD and branch references, which is why it can recover 'lost' states.
2. After git reset --hard HEAD~3, the discarded commits are:
They become unreachable but remain in the reflog and recoverable until garbage collection prunes them.
3. Is the reflog shared with teammates when you push?
The reflog is entirely local; it is never transferred by clone, fetch, or push.
Flash Cards
What is git reflog? — A local log of every position HEAD and branch refs have pointed to, used to recover lost commits.
Recover after git reset --hard — Run git reflog, find the pre-reset SHA (e.g. HEAD@{1}), then git reset --hard HEAD@{1} or git branch <name> <sha>.
reflog vs log — log shows commit ancestry; reflog shows the chronological movement of HEAD, including states no branch points to.
How long do entries last? — Reachable entries default to 90 days, unreachable to 30, before git gc can prune them.
Continue Learning
Related Interview Questions
You ran git reset --hard and lost committed work. How do you get it back?
medium
What is the difference between git reset, git revert, and git checkout?
medium
How does a Git branch work internally and why is branching cheap in Git?
medium
How do you undo the last commit in Git without losing your changes?
easy