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, first confirm that all of them belong to you and that you intend to restore them later:

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

If status includes edits owned by someone else or untracked files with an uncertain source, stop in this worktree, preserve the state, or use a separate worktree.

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:

<integration-branch> is the team’s actual integration branch, such as main, master, or develop:

git show --stat
git diff <integration-branch>...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