Recover a Lost Commit with Git Reflog
A missing branch tip is not necessarily a missing object
A private branch contains commits A and B. An accidental reset leaves main at A, so ordinary history of main no longer includes B. The local reflog may still record B as an earlier reference position. Reflogs are local, can be disabled or expired, and are not replicated by ordinary clone or push. First stop unnecessary writes and maintenance; preserve the repository and valuable work independently. Use the lost scenario, which already simulates the reset.
Locate and inspect a candidate
HEAD reflog includes checkout movements as well as commits and other updates. A branch reflog describes updates to that branch. In the controlled scenario HEAD@{1} and main@{1} both resolve to B; do not assume that numbering in real recovery. Select by message, time and content; resolve the selector to a full ID before more operations change its meaning. ORIG_HEAD may help immediately after reset but is overwritten by other operations.
git reflog show HEADgit reflog show maincandidate=$(git rev-parse --verify "HEAD@{1}^{commit}")git show --stat "$candidate"git show "${candidate}:notes.txt"Anchor first, integrate later
After confirming B contains the intended change, create a new branch at the candidate. This does not switch branches or change current files. Inspect the recovered snapshot and compare it with main before choosing integration. A named reference protects reachable objects under ordinary maintenance, but is still inside the same repository.
git branch rescue-recovered "$candidate"git show rescue-recovered:notes.txtgit diff main rescue-recoveredgit status --shortRecover without guessing
In the fixture main still contains A while rescue-recovered contains B. No force push or second reset is needed. If a candidate object is missing, a reflog line alone cannot reconstruct it; look for authorized independent clones or backups. Practice: explain why git log main fails to display B but git log rescue-recovered displays it.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Recover a Lost Commit with Git Reflog — knowledge check
Reason about reset states and safe recovery.
Why can B be absent from git log main but still exist?
Is HEAD@{1} always the lost commit?
What should you verify before creating a rescue branch?
Does git branch rescue-recovered ID switch HEAD?
Are reflogs copied by ordinary clone?