Git Reflog: Recover Lost Commits
Learn how to use git reflog to recover lost commits, undo a bad rebase, or restore deleted branches. Your ultimate safety net in Git.
git reflogDetailed Explanation
What Does git reflog Do?
git reflog (reference log) records every time the tip of a branch changes in your local repository. Unlike git log, which follows the commit ancestry, reflog tracks HEAD movements — including resets, rebases, checkouts, and even commits on branches that were deleted.
Why Reflog Matters
When you run git reset --hard or delete a branch, those commits seem to vanish from git log. But Git does not immediately garbage-collect them. The reflog retains references to these "orphaned" commits for at least 90 days by default.
Viewing the Reflog
git reflog
# Output:
# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
# f4e5d6a HEAD@{1}: commit: Add payment validation
# 7g8h9i0 HEAD@{2}: commit: Update user model
Recovering a Lost Commit
# Find the commit hash in reflog
git reflog
# Create a new branch pointing to the lost commit
git branch recovered-work f4e5d6a
# Or reset your current branch to that commit
git reset --hard f4e5d6a
Recovering a Deleted Branch
# Find where the branch tip was
git reflog | grep "checkout: moving from feature-x"
# Recreate the branch
git branch feature-x <hash>
Undoing a Bad Rebase
If a rebase goes wrong, find the pre-rebase state in the reflog and reset to it:
git reflog
# Find the entry just before the rebase started
git reset --hard HEAD@{5}
Limitations
Reflog is local only — it is not shared with remotes or collaborators. It is also subject to garbage collection via git gc. For critical recovery, act promptly. Think of reflog as your local undo history: powerful, but not a substitute for proper backups.
Use Case
A developer accidentally ran git reset --hard and lost three commits. They use git reflog to find the commit hashes and restore the lost work.