git reflog
I ran git reset --hard on the wrong branch once and spent a genuinely unpleasant ten minutes before somebody told me about this.
git reflog
It prints every position HEAD has held in this repository, most recent first, with the command that moved it. Resets, checkouts, merges, rebases, commits, all of it.
a1b2c3d HEAD@{0}: reset: moving to HEAD~3
9f8e7d6 HEAD@{1}: commit: Add the delivery cutoff check
The commit I thought I had destroyed is 9f8e7d6, sitting right there. git checkout -b recover 9f8e7d6 and it is back.
Two things worth knowing about it. It is local and personal, so it is not in the clone your coworker has and it does not survive a fresh clone of your own. And entries expire, ninety days by default for reachable commits and thirty for unreachable ones. So this is a safety net for last week rather than for last year.
The mental model that stuck for me: committing in git is close to permanent, and nearly every command that looks destructive is only moving a pointer. The commit is still on disk with nothing pointing at it. reflog is the list of things that used to point at it.
Which is also, from the other direction, exactly why a password committed once is a problem forever.