Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Git reflog: Recovering Lost Commits and Deleted Branches

Every ref movement is logged locally for ninety days. How to undo a reset, restore an amended commit, and what fsck can still find when the log is gone.

~5 min readGit
#Git#Recovery#Reflog#CLI
3D illustration of a graveyard of faded commit blocks with a glowing reflog trail back

Quick Summary

The reflog is a local, append-only log of where each ref has pointed. Every commit, merge, rebase, reset, checkout, and amend writes an entry, so a branch you deleted or a commit you rewrote is almost always still reachable — for 90 days by default for reachable entries, 30 days for unreachable ones.

Two things make it indispensable. First, git reflog shows HEAD movement, which is what you need after "my branch is gone". Second, any reflog entry can be used as a revision: HEAD@{1}, main@{yesterday}, stash@{2}.

What the reflog is not: it is not shared. A fresh clone has one entry per branch pointing at the clone-time tips, and no history of what happened on the origin. Recovery of anything that was never on your machine comes from someone else's clone, not from your reflog.

Basic Syntax and Core Signatures

bash
git reflog # HEAD movement, newest first git reflog show main # one branch git reflog show --all --date=iso git reflog expire --dry-run --expire=now --all # what would be pruned # Any reflog entry is a revision git show HEAD@{1} git diff main@{yesterday} main git switch -c rescue HEAD@{3} git reset --hard HEAD@{1} # undo the last reset git branch recover main@{2} # Config git config --global gc.reflogExpire 90.days git config --global gc.reflogExpireUnreachable 30.days # Dangling and unreachable objects: the fallback when the reflog is gone git fsck --lost-found # writes dangling commits to .git/lost-found/commit/ git fsck --unreachable --no-reflogs git show <dangling-sha>

A reflog entry looks like this, and the middle columns are the operation that created it:

text
3f2a1b9 HEAD@{0}: reset: moving to HEAD~1 9c1e2f4 HEAD@{1}: commit: feat: add retry budget 5d6e7f8 HEAD@{2}: rebase (finish): returning to refs/heads/main

Key Comparison Table

ReferenceResolves toSurvives a cloneChanges when
HEAD~2Second ancestor of HEADYesCommits change
HEAD@{2}Where HEAD was two moves agoNo, local onlyAny ref movement
main@{yesterday}main at the end of that dayNoDaily granularity
@{-1}Previously checked-out branchNoSwitching
refs/stashNewest stash commitNoStash push
Dangling commitCommitted object with no refObjects may be copied by a clone only if reachableGarbage collection
SituationRecovery command
Branch deleted, was not mergedgit branch <name> <sha from reflog>
reset --hard discarded commitsgit reset --hard HEAD@{1}
Amended commit replacedgit switch -c fix <old sha> from reflog
Rebase lost the original branchgit reflog, then git reset --hard HEAD@{n}
Dropped stashgit fsck --unreachable, then git stash apply <sha>
Commits from a deleted remote branchA colleague's clone, or the hosting service's API

Practical Edge Cases and Deep Dives

1. Recovering a deleted branch

git branch -D deletes the ref, not the commits. The reflog for HEAD shows the last commit that branch pointed at, provided you were on it at some point:

bash
git reflog --date=iso | grep -i "feature/login" # 9c1e2f4 HEAD@{4}: commit: feat: rate limit login git branch feature/login 9c1e2f4

If you never checked the branch out, its reflog still exists under git reflog show feature/login until the entry expires — but after deletion there is no name to query. git fsck --lost-found is then the only route, and it lists every dangling commit, which is a long list on a busy repository.

2. Undoing a reset the wrong way

A reset moves HEAD and writes a reflog entry for the reset itself, so HEAD@{1} is the state before it. Two consecutive resets mean HEAD@{2}. Reading the reflog before acting is the whole technique:

bash
git reflog -8 # 3f2a1b9 HEAD@{0}: reset: moving to HEAD~1 <- the mistake # 9c1e2f4 HEAD@{1}: commit: feat: add retry budget <- what you want back git reset --hard HEAD@{1}

git reset --hard HEAD@{1} after a rebase returns to the pre-rebase tip, including the original commit hashes.

Two details make the fix reliable. The reflog entry you want is the one whose operation starts with commit: or rebase (start):, not the one created by the reset itself. And HEAD@{1} is stable only until your next Git command — every operation appends an entry, so act before running anything else, or copy the hash out of git reflog and use the hash directly. Hashes do not shift; indices do.

3. Recovering an amended commit

git commit --amend creates a new commit and moves the branch; the old one is still in the reflog as the previous entry. Recovering it means branching from it and diffing against the amended version to see what was lost:

bash
git reflog -3 git switch -c recovered 5d6e7f8 git diff recovered main

4. Expiry, garbage collection, and what is actually permanent

Reachable reflog entries expire after gc.reflogExpire (90 days); entries pointing at commits no longer reachable expire after gc.reflogExpireUnreachable (30 days). After that, git gc can delete the objects. CI machines and fresh clones also effectively reset the clock, which is why a repository re-cloned weekly has short recovery windows.

bash
git gc --prune=now --aggressive # destructive; never run to "clean up" before a recovery

Previous-reference forms. Three different "previous" references with different meanings. HEAD@{1} is the previous position of HEAD in the reflog, regardless of operation. @{-1} is the previously checked-out branch, which is what git switch - uses. ORIG_HEAD is set by operations that rewrite history — merge, rebase, reset — and holds the pre-operation tip. Scripts that meant "undo the merge" should use ORIG_HEAD, which is less ambiguous than counting reflog entries.

5. The reflog on the server is not yours

A remote's reflog records pushes to that remote's refs, and you cannot read it without shell access to the server. That means a force-push that overwrote a colleague's commits is recoverable only if their clone still has them, or the hosting provider keeps the old ref (GitHub exposes it through the events API and the compare view for a limited time). The local reflog protects local work; protecting shared work is a matter of policy, not of Git. A team that force-pushes without a lease is one accidental overwrite away from a lost afternoon, and the reflog only helps the person who still has the entry.

None of this changes how a team should work. A force-push to a shared branch is still a rewrite of someone else's history, and the fact that it can be recovered from a laptop does not make it a low-cost action; the cost is a colleague's afternoon, and it lands on them rather than on you.

6. Reading a reflog as a history of intent

Beyond recovery, the reflog is the only record of operations that leave no trace in git log — resets, checkouts, rebases, amends. When a colleague reports "something happened to my branch last Tuesday", git reflog --date=iso is the first command to run, and git diff <sha>@{day before} <sha>@{day after} shows what changed. On a shared machine or a build agent, the reflog also explains the mysterious extra commits a deployment script left behind, because each push and reset appears with its timestamp.

bash
git reflog --date=iso main | head -20 git log --oneline --since="2026-11-12 00:00" --until="2026-11-13 00:00" main

Common Git Operation Mistakes

SymptomCauseFix
"fatal: ambiguous argument 'HEAD@{1}'"No prior reflog entry, or the shell ate the bracesQuote the argument, check git reflog length
Recovered commit is empty or unrelatedWrong index into a long reflogRead the entries with --date=iso, match the message
fsck lists hundreds of commitsBusy repository, many dangling objectsFilter by date or author, inspect with git show
Reflog gone after re-cloneReflog is localRecover from the origin or a colleague's clone
Force-push destroyed remote commitsOverwrote a shared refPartner's clone, provider event log
git gc deleted the objects before recoveryExpiry passed, or --prune=now usedNothing left; restore from a backup
Branch restored at the wrong tipPicked a reset entry instead of the commit entryRead the operation column
@{yesterday} resolves oddlyLocal timezone and day boundaryUse --date=iso with HEAD@{<sha>} or a timestamp

Try matching browser tools

All tools

Small utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Git guides

More in Git

All Git guides

The guides filed next to this one in Git, in library order.