By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
What it is:Git’s reflog (reference log) is a hidden, time-stamped journal of every change to your branch tips (HEAD, main, feature/x, etc.). It records every Git operation—commits, resets, merges, rebases, checkouts—even if those changes don’t appear in git log. Think of it as Git’s "undo history" for your repository’s state.
HEAD
main
feature/x
git log
Why it matters in production:- You just force-pushed (git push --force) and wiped out a teammate’s work. Without reflog, that commit is gone forever.- You ran git reset --hard and lost a day’s work. Reflog lets you recover it.- A rebase went wrong, and now your branch is broken. Reflog helps you rewind to a safe state.- You accidentally deleted a branch. Reflog can resurrect it.
git push --force
git reset --hard
Real-world scenario:You’re working on a critical feature branch. A teammate asks you to rebase onto main to clean up the commit history. You run git rebase main, but something goes wrong—conflicts explode, commits disappear, and now your branch is a mess. Without reflog, you’d have to manually reconstruct the lost commits. With reflog, you can undo the rebase in seconds.
git rebase main
git reflog
Production insight: Reflog is not shared with remote repos. If you lose commits after a git push --force, reflog won’t help unless you have a local copy.
HEAD@{n}
HEAD@{5}
Production insight: These references are temporary—they expire after 30 days (configurable via gc.reflogExpire).
gc.reflogExpire
git reflog show <branch>
git reflog show feature/login
Production insight: Useful when debugging branch-specific issues (e.g., "Why did feature/login suddenly point to an old commit?").
feature/login
git fsck --lost-found
Production insight: Slower than reflog but useful if reflog entries have expired.
git cherry-pick <commit>
Production insight: Use this to recover specific lost commits without rewriting history.
git reset --hard <commit>
Production insight: Always check git reflog before running this—it’s irreversible without reflog.
git branch <new-branch> <commit>
Production insight: The safest way to recover lost work—no risk of overwriting existing branches.
gc.reflogExpire (Git config)
git config --global gc.reflogExpire "90.days.ago"
git checkout
git reset
You ran git reset --hard HEAD~3 and lost 3 commits. Here’s how to recover them.
git reset --hard HEAD~3
Output:
a1b2c3d (HEAD -> main) HEAD@{0}: reset: moving to HEAD~3 e4f5g6h HEAD@{1}: commit: Add user authentication i7j8k9l HEAD@{2}: commit: Fix login bug m1n2o3p HEAD@{3}: commit: Refactor API client q4r5s6t HEAD@{4}: checkout: moving from feature/login to main
e4f5g6h
i7j8k9l
m1n2o3p
Option A: Recover the entire branch (safest)
git branch recovered-branch m1n2o3p # Create a new branch from the lost commit git checkout recovered-branch
Option B: Cherry-pick a single commit
git cherry-pick e4f5g6h # Reapply just the "Add user authentication" commit
Option C: Reset to the lost commit (destructive)
git reset --hard m1n2o3p # WARNING: Overwrites current branch
git log --oneline
Expected output:
m1n2o3p Refactor API client i7j8k9l Fix login bug e4f5g6h Add user authentication
git push origin recovered-branch
You ran git push --force and overwrote a teammate’s work. The remote is now broken.
git reflog show origin/main # Check remote-tracking branch
a1b2c3d (origin/main) HEAD@{0}: push: forced update e4f5g6h HEAD@{1}: commit: Teammate's critical fix
git checkout -b fix-recovery e4f5g6h
git push origin fix-recovery
fix-recovery
master
bash git config --global gc.reflogExpire "90.days.ago" git config --global gc.reflogExpireUnreachable "30.days.ago"
git tag backup-before-rebase
git fsck
bash git fsck --lost-found git show <dangling-commit-hash> # Inspect before recovering
git fetch origin
git log origin/main..main
git reflog show origin/main
--force-with-lease
--force
bash git push --force-with-lease origin main
git fetch
git reflog show origin/branch
git branch recovered-branch <commit>
Trap: "Use git log" (wrong—git log only shows current branch history).
"A teammate force-pushed to main and overwrote your work. How do you restore it?"
git checkout -b fix <lost-commit>
Trap: "Clone the repo again" (wrong—this won’t help if the remote is already overwritten).
"What’s the difference between git reflog and git log?"
Trap: "They’re the same" (wrong—reflog is a superset of git log).
"How do you prevent accidental force-pushes?"
git checkout -b temp-work
bash echo "test" > file.txt git add file.txt && git commit -m "First commit" echo "more work" >> file.txt git add file.txt && git commit -m "Second commit"
git checkout main && git branch -D temp-work
git reflog # Find the commit where temp-work was deleted git branch temp-work <commit-hash> # Recreate the branch git checkout temp-work
Why it works: The reflog records the branch deletion, so you can recreate it from the last known commit.
git checkout -b <new-branch> <commit>
git checkout -b fix e4f5g6h
git cherry-pick i7j8k9l
git reset --hard HEAD@{5}
git push --force-with-lease
git push --force-with-lease origin main
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.