Interview Prep/Git & GitHub/Git internals: objects, refs and the index

What is a detached HEAD and how do you recover work done in it?

IntermediateConceptGit internals: objects, refs and the index
Answer

HEAD contains a raw commit ID instead of a branch name, which happens when you check out a tag, a commit or a remote-tracking branch. Commits made there are not reachable from any branch and can be garbage collected eventually. To keep them, create a branch before leaving.

git switch -c rescue            # while still detached
git reflog                      # if you already moved away
git branch rescue <sha-from-reflog>
All Git & GitHub questions Roadmap: Git & GitHub

More on Git internals: objects, refs and the index