How to Recover a Git Commit After a Reset
A reset can make a commit disappear from your branch history without immediately deleting the commit object. If you moved a branch or ran git reset and now cannot find yesterday’s work, pause before running cleanup commands. Git’s reflog often shows where HEAD pointed before the change.
Editorial illustration: find the old commit, protect it with a branch, then inspect it.
Summary
- A reflog records recent movements of
HEADand local branch references. - Find the missing commit with
git reflog, then create a rescue branch before changing history again. - Reflog recovery applies to commits Git still knows about; it cannot restore arbitrary uncommitted files that were never saved as commits or stashes.
First, stop changing the repository
Do not run git gc, delete .git, or keep experimenting with resets. If you have uncommitted work now, copy it somewhere safe or make a temporary commit before trying recovery. That precaution does not restore the lost commit; it protects the work that is still visible.
Check what Git currently sees:
git status --short --branch
git log --oneline --decorate -12
A clean working tree makes the next steps easier to reason about. If the status shows changes you care about, preserve them first. Avoid git reset --hard as a discovery tool: it changes the working tree and can discard edits.
Step 1: Read the reflog
Run:
git reflog --date=local
You may see entries such as:
9d4c123 HEAD@{0}: reset: moving to HEAD~1
2ab8f90 HEAD@{1}: commit: add import validation
The hash at HEAD@{1} is an example, not a value to copy. Find the entry whose message and surrounding sequence fit what you did. A commit message might appear in the reflog even when the current branch no longer points to it.
If the operation happened on another local branch, inspect that branch’s reflog too:
git reflog show feature/search --date=local
Reflog entries are local records. They are not a shared server history and do not prove that a commit was pushed.
Step 2: Protect the candidate before inspecting it
Copy the candidate hash from your own output and create a rescue branch:
git branch rescue/recovered-work 2ab8f90
Replace the sample hash with the candidate you found. This branch gives the commit a name, so later repository activity is less likely to leave it unreachable. If the branch name already exists, choose another name rather than moving an existing rescue branch blindly.
Now inspect the commit without checking it out:
git show --stat --oneline rescue/recovered-work
git show --format=fuller --no-ext-diff rescue/recovered-work
Check the files and diff. If this is the work you expected, your commit has been recovered from Git’s local history. Your current branch has not yet been changed.
Step 3: Decide how it belongs in your work
There is no single “restore” command that is safe for every case. Pick the smallest action that matches the repository state.
You want to continue from that commit: after saving any current changes, check out the rescue branch and continue there.
git switch rescue/recovered-work
You want its changes on your current branch: cherry-pick the commit after confirming it is not already an ancestor and the working tree is ready.
git switch my-current-branch
git cherry-pick rescue/recovered-work
If conflicts occur, resolve and test them before continuing. A cherry-pick creates a new commit with the same changes; it does not move the old commit back into place.
You only need a file from it: inspect the path first, then restore just that path from the rescue branch. This avoids replacing unrelated current work.
git restore --source=rescue/recovered-work -- path/to/file
Review git diff before committing. Do not use a hard reset to make the branch “look right” unless you intentionally want to rewrite the branch and understand the consequences for collaborators.
What if the reflog does not show it?
Search other local references and branches first:
git branch -a --contains 2ab8f90
git fsck --no-reflogs --unreachable
git fsck can report dangling commits, but its output may be large and requires careful inspection. Do not prune objects while investigating. You can inspect a candidate with git show <hash> and protect a useful one by creating a branch at that hash.
If the commit was pushed, check the remote branch or ask a teammate before rewriting shared history. If it exists in another clone, that clone may still have it. If there was no commit, stash, patch, editor history, or other saved copy, reflog cannot reconstruct edits that Git never recorded. Reflog is a record of reference movement, not a backup of every keystroke.
Common mistakes to avoid
- Resetting again before making a rescue branch. Each change makes the timeline harder to read.
- Assuming
HEAD@{1}is always the answer. It is just the previous reflog position; inspect dates, messages and diffs. - Cherry-picking without checking for duplicate changes. The commit may already be present under a different hash.
- Confusing a lost commit with lost uncommitted files. A commit can often be found; discarded, never-saved edits may not be recoverable through Git.
- Force-pushing to “bring it back.” Coordinate first if other people use the branch. A force push can remove their work from the shared branch view.
Bottom Line
Run git reflog, identify the likely commit, create a rescue branch at its hash, and inspect the diff. Only then decide whether to switch branches, cherry-pick, or restore one file. The rescue branch is the safety line; the history rewrite is optional.
My Take
When a reset surprises you, the most useful first move is not to fix the branch. It is to name the work you might have lost. A rescue branch turns a worrying hash into something stable that you can inspect calmly.