Git Notes
Master git reflog to recover lost commits, understand commit references, restore work after accidental resets or deletions, and use reflog for emergency recovery.
Every developer has experienced that moment of pure panic. You ran git reset --hard and watched your last three days of work vanish. Or you accidentally deleted a branch with unmerged commits. Your stomach drops, and you start calculating how long it will take to redo everything from memory.
Here is the good news: in almost every scenario involving "lost" commits, Git has not actually deleted anything. The commits still exist in the object database. They just became unreachable — no branch or tag points to them anymore. The tool that lets you find these orphaned commits is git reflog, and understanding it will save your career at least once.
Understanding the Reflog
The reflog (reference log) records every time HEAD moves in your repository. Every commit, checkout, reset, rebase, merge, and pull updates HEAD, and each update gets logged. It is essentially a chronological diary of everything you have done:
git reflog
# Output:
# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
# e4f5g6h HEAD@{1}: commit: feat: complete payment integration
# i7j8k9l HEAD@{2}: commit: feat: add stripe webhook handler
# m1n2o3p HEAD@{3}: commit: feat: setup payment models
# q4r5s6t HEAD@{4}: checkout: moving from main to feature/payments
# u7v8w9x HEAD@{5}: pull: Fast-forwardEach entry shows: the commit hash, a reflog reference (HEAD@{n}), the action that caused the movement, and a description. The HEAD@{0} is always the current state, HEAD@{1} is where HEAD was before the last operation, and so on.
Recovery Scenario 1: Accidental Hard Reset
This is the most common disaster. You meant to reset one commit but accidentally went back too far:
# The disaster
git reset --hard HEAD~5
# Just lost 5 commits of work!
# Step 1: Check reflog immediately
git reflog
# a1b2c3d HEAD@{0}: reset: moving to HEAD~5
# e4f5g6h HEAD@{1}: commit: latest feature work <- This is what you lost!
# Step 2: Reset back to the commit before the accident
git reset --hard e4f5g6h
# All five commits are back! Crisis averted.
git log --oneline
# e4f5g6h feat: latest feature work
# i7j8k9l feat: add webhook handler
# m1n2o3p feat: setup models
# ... all five commits restoredRecovery Scenario 2: Deleted Branch
You force-deleted a branch thinking it was merged, but it was not:
# The disaster
git branch -D feature/critical-fix
# Deleted branch feature/critical-fix (was a1b2c3d).
# Thankfully Git tells you the hash! But even if you missed it:
git reflog
# Look for the last commit on that branch
# Or check the branch-specific reflog (if it still exists)
git reflog show feature/critical-fix
# Recreate the branch
git branch feature/critical-fix a1b2c3d
# Or create it and switch to it
git checkout -b feature/critical-fix a1b2c3dRecovery Scenario 3: Bad Rebase
Interactive rebase went wrong and your commit history is mangled:
# The disaster
git rebase -i HEAD~10
# Something went wrong, commits are in wrong order,
# some are missing, squash went badly
# Step 1: Find pre-rebase state in reflog
git reflog
# a1b2c3d HEAD@{0}: rebase (finish): refs/heads/feature onto main
# e4f5g6h HEAD@{1}: rebase (squash): combined commit
# i7j8k9l HEAD@{2}: rebase (pick): some commit
# m1n2o3p HEAD@{8}: rebase (start): checkout main
# q4r5s6t HEAD@{9}: commit: my last commit before rebase <- HERE
# Step 2: Reset to pre-rebase state
git reset --hard q4r5s6t
# Back to exactly where you were before the rebaseRecovery Scenario 4: Lost Stash
When you drop or clear stashes, they become orphaned commits:
# The disaster
git stash drop stash@{0}
# Dropped stash@{0} (a1b2c3d4e5f6...)
# Recovery: the hash was shown! But if you missed it:
git fsck --unreachable | grep commit
# unreachable commit a1b2c3d4e5f6...
# unreachable commit x9y8z7w6v5u4...
# Apply the lost stash
git stash apply a1b2c3d4e5f6Recovery Scenario 5: Undoing a Bad Merge
You merged the wrong branch and want to get back to pre-merge state:
# You accidentally merged develop into your feature branch
git merge develop # Oops!
# Reflog shows the pre-merge state
git reflog
# a1b2c3d HEAD@{0}: merge develop: Merge made by...
# e4f5g6h HEAD@{1}: commit: my feature work <- Pre-merge state
# Reset back
git reset --hard HEAD@{1}
# or
git reset --hard ORIG_HEAD # Git stores pre-merge HEAD automaticallyUsing git fsck for Deep Recovery
When reflog does not have what you need (entries expired or you need to find truly orphaned objects):
# Find all unreachable commits
git fsck --unreachable --no-reflogs
# Output:
# unreachable commit a1b2c3d4e5f6...
# unreachable commit x9y8z7w6v5u4...
# unreachable blob m1n2o3p4q5r6...
# Inspect each unreachable commit
git show a1b2c3d4e5f6
# See its message and diff to identify if it is the one you need
# Recover it
git branch recovered a1b2c3d4e5f6Reflog Retention and Garbage Collection
Reflog entries do not live forever. Understanding retention is crucial for knowing your recovery window:
# Default retention: 90 days for reachable, 30 days for unreachable
git config gc.reflogExpire # Default: 90 days
git config gc.reflogExpireUnreachable # Default: 30 days
# Extend retention if you want more safety
git config --global gc.reflogExpire 180
git config --global gc.reflogExpireUnreachable 90
# Manually trigger garbage collection (removes expired entries)
git gc
# Check when garbage collection last ran
git count-objects -vAfter garbage collection removes a commit from the object database, it is truly gone. This is why the recovery window matters.
Important Limitations
The reflog has limitations you must understand:
- Local only: Reflog exists only on your machine. It does not sync to GitHub. If your hard drive dies, the reflog is gone.
- Per-repository: Each clone has its own independent reflog. Your teammate's reflog will not help you.
- Time-limited: Entries expire after 90 days by default. Old accidents cannot be recovered.
- Not a backup: Reflog is a recovery tool, not a backup strategy. Always push important work to a remote.
Best Practices for Avoiding Data Loss
# Push frequently to have a remote backup
git push origin feature/my-work
# Before dangerous operations, create a backup branch
git branch backup/before-rebase
git rebase -i HEAD~5
# If rebase goes wrong, you have the backup branch
# Use --keep-backup option with some tools
git filter-branch --backup ...
# Set longer reflog retention
git config --global gc.reflogExpire 180Interview Questions
Q1: How does git reflog help recover lost commits?
The reflog records every HEAD movement, creating a chronological log of all states your repository has been in. When commits become unreachable (through reset, rebase, or branch deletion), you can find their hashes in the reflog and reset or branch from those hashes to recover them. It is the primary disaster recovery mechanism in Git.
Q2: What is the difference between git log and git reflog?
git log shows the commit history reachable from the current branch — it follows parent pointers. git reflog shows the chronological history of where HEAD pointed, including states that are no longer reachable from any branch. Reflog catches everything log misses after history-rewriting operations.
Q3: Can you recover from git reset --hard?
Yes, within the reflog retention period (default 90 days). Use git reflog to find the commit hash before the reset, then git reset --hard <hash> to restore. The commits were never deleted from the object database; they just became unreachable from any branch pointer.
Q4: What are the limitations of reflog for recovery?
Reflog is local only (does not sync to remotes), entries expire after 90 days (30 for unreachable commits), and once garbage collection removes the underlying objects, recovery is impossible. It is also per-repository, so a fresh clone has an empty reflog.
Q5: What happens to reflog entries after garbage collection?
Expired reflog entries are removed during git gc. Once the reflog entry is gone and no other reference points to the commit, the commit object itself is removed from the object database, making true recovery impossible. This is why acting quickly after discovering data loss improves recovery chances.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Recover Lost Commits - Reflog Guide.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, undoing, changes, recover, lost
Related Git & GitHub Topics