Recover commits after git reset --hard
Safe: nothing is lost
Short answer
Run git reflog to find where your branch was, then git reset --keep HEAD@{1} to go back there.
The commands, one by one
- 1
git reflogHEAD@{0}is the reset you just did;HEAD@{1}is where you were right before it, with your commits. - 2
git reset --keep HEAD@{1}Moves the branch back to that point.
--keeprefuses if it would overwrite uncommitted changes.
For your own repository: If you did other things since, pick the right HEAD@{n} from the reflog list (the line before the reset).