Git Reflog
Git Reflog: Your Local Safety Net for Undoing Mistakes
git reflog records a local chronological log of every change to HEAD and branch tips—even commits orphaned by git reset --hard, an aborted rebase, or an accidentally deleted branch. As long as a commit was committed locally at least once, git reflog usually lets you bring it back in seconds.
Reading git reflog Output
Run git reflog in any repository to see recent pointer movements ordered from newest (HEAD@{0}) to oldest:
git reflog -n 10
Each line prints a short commit hash, a relative reference like HEAD@{2}, the action (commit, reset, rebase, checkout), and the commit subject line. Notice that git log only shows commits reachable from your current branch tip, whereas git reflog shows every commit HEAD visited locally.
Recovering After an Accidental git reset --hard
Suppose you ran git reset --hard HEAD~2 by mistake and wiped two good commits off feature/header-nav. Inspect the reflog to locate the commit hash right before the reset (HEAD@{1}), then restore your branch:
git reflog
git switch -c recovered-header HEAD@{1}
Creating a new branch at the recovered hash first is the safest habit because you can inspect the files before resetting feature/header-nav back to that point.
Recovering a Deleted Unmerged Branch
If you ran git branch -D feature/header-nav too early, find its last commit hash in git reflog and recreate the branch pointer immediately:
git branch feature/header-nav 7d4e91c
Common Reflog Misconceptions
The reflog is strictly local to your own machine and is never shared with origin via git push. Also remember git reflog cannot recover edits that were never committed or stashed (such as unsaved working directory edits wiped by git reset --hard).
Frequently Asked Questions
How long does Git keep entries in git reflog?
By default, Git keeps reachable reflog entries for 90 days and unreachable (orphaned) commit entries for 30 days before git gc garbage-collects them.
Can I check the reflog of a specific branch instead of HEAD?
Yes. Run git reflog show feature/header-nav to see every update to that specific branch pointer.
Does git reflog restore uncommitted file changes?
No. git reflog only tracks committed snapshots (and stash commits). Uncommitted changes deleted with git reset --hard or git restore are not recorded in the reflog.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With