Skip to content

Recover commits with git reflog

The reflog records where local references such as HEAD and branches pointed recently. It can help recover commits after a reset, rebase, or local branch deletion.

Find the previous position

git reflog

Look for the commit ID and description from before the mistake. Inspect it before recovering:

git show <commit>

Create a recovery branch

git branch recovery/<short-description> <commit>
git switch recovery/<short-description>

A recovery branch gives the commit a name so it is not lost while you investigate.

Local record only

Reflog entries are local and expire. They are not available on a remote just because the commit was once pushed there.

Recover a deleted branch

If a local branch was deleted, locate its last commit in the reflog and recreate it:

git reflog --all
git branch <branch-name> <commit>

Do not run cleanup commands until important commits have been recovered and verified.