git reset --hard makes commits vanish from git log, but git keeps them. The reflog is git's diary of every commit HEAD has been on, newest first:
git reset --hard HEAD~1
HEAD is now at 3338d7e Add a banner
git reflog
3338d7e HEAD@{0}: reset: moving to HEAD~1
a731028 HEAD@{1}: commit: Add a FAQ ← where HEAD was before the reset
3338d7e HEAD@{2}: commit: Add a banner
c45945c HEAD@{3}: commit (initial): Start the shopHEAD@{1} means "where HEAD was one move ago". Reset back to it, or to its hash:
git reset --hard HEAD@{1}
HEAD is now at a731028 Add a FAQ
git log --oneline
a731028 (HEAD -> main) Add a FAQ
3338d7e Add a banner
c45945c Start the shopEvery switch, commit and reset adds a line at the top, so the HEAD@{n} numbers shift. Run git reflog right before you use one, or use the hash instead.
The reflog lives only in your clone, and git cleans out very old entries after a few months. For anything recent, it's the way back.
Objectives
- Find the commit
mainwas on before the reset. - Bring both lost commits back.