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
List everywhere HEAD has been:
$ git reflogPoint your branch back at the lost commit:
$ git reset --hard a1b2c3dOr 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.