my-git

Recover Lost Commit

English 中文

When you lose a commit, check reflog first.

Many “lost” commits are actually still local, just that the current branch pointer no longer points to them.

Capture the Current State

git status
git log --oneline --decorate -10
git reflog -20

If there are still uncommitted changes in the working tree:

git stash push -u -m "backup before recover lost commit"

Find Commit Using Reflog

git reflog

After finding the target commit, do not rush to reset; first create a recovery branch:

git branch recovered-work <commit-sha>
git switch recovered-work

This allows you to check the content first:

git show --stat
git diff main...HEAD

Common Recoverable Scenarios

When Recovery Might Be Impossible

If it was never committed, Git usually cannot save it; you need to look at editor history, local backups, or IDE local history.

Further Reading