Git internalsmedium3-5 years

A developer runs `git reset --hard HEAD~1` on the wrong branch, wiping out a commit they needed. `git log` no longer shows it. Is the commit actually gone, and how would you get it back?

It isn't gone — git reset --hard moves the branch pointer and rewrites the working tree, but it doesn't delete the commit object itself, which still exists in .git/objects under its original hash. git log only shows commits reachable by walking parent pointers backward from wherever the current branch points, and after the reset, the branch no longer points anywhere near the wiped commit, so git log genuinely can't find it by that walk — but "not found by this walk" isn't the same as "destroyed." git reflog is a separate, local record of every place HEAD has pointed, including right before the reset, so it will show the wiped commit's hash directly. git reset --hard <that hash> puts the branch back exactly where it was.

The lesson behind it →
More on Git internals