Dev Journal

Getting a lost commit back with git reflog

23 October 2026

After a reset --hard or a rebase that went wrong, the branch no longer points at the commits you wanted. They are not deleted yet. Git records every move of HEAD in the reflog, and you can read it:

git reflog

Each line shows an old position of HEAD with a short hash and a note about what happened, newest first. Find the line from before the mistake. You can inspect it with git show, and the safest way to recover is to put a new branch on it instead of moving the current one:

git switch -c rescue 7a1c3e9

If you are sure, resetting the current branch to that entry also works, using the position syntax:

git reset --hard HEAD@{3}

Reflog entries are local to your clone and they expire, by default after 30 days for unreachable commits and 90 days for reachable ones. They do not exist on a fresh clone. So the habit is to look right after you notice the problem, and to create a branch before doing anything else that rewrites history.