Git internalseasy0-2 years

A developer runs `git checkout <some old commit hash>`, makes a few commits while poking around, then switches back to `main` without naming anything. Are those commits gone? What actually happened?

The commits aren't deleted, but they are effectively lost unless recovered quickly: checking out a commit hash directly (instead of a branch name) puts HEAD into "detached" state, meaning .git/HEAD holds a raw commit hash instead of a reference to a branch file. Any commits made in that state are real, valid objects, written exactly like any other commit — but no branch file points at them. The moment the developer switches to main, nothing in the repository's normal ref set names those commits anymore, so they become unreachable: still physically present in .git/objects, but with no name to find them by, and git log won't show them. They aren't destroyed, but without recovering the hash — from the terminal scrollback or git reflog — they'll eventually be garbage-collected.

The lesson behind it →
More on Git internals