How to recover a lost commit with git reflog

Run git reflog to see every commit HEAD has pointed to, newest first. Find the one you lost, then run git reset --hard <commit-id> (or git branch rescue <commit-id>) to bring it back.

Step by step

  1. List everywhere HEAD has been:

    $ git reflog
  2. Point your branch back at the lost commit:

    $ git reset --hard a1b2c3d
  3. Or keep your branch and save the commit on a new one:

    $ git branch rescue a1b2c3d

The reflog is local to your computer and entries expire after about 90 days, so commits are almost never truly lost right after a mistake.

Try it in the interactive sandbox →Practice “Oh no” with a real repository and teammates, right in your browser.

Related guides