Frequently Asked Question
What is a detached HEAD and how do I get out of one?
A detached HEAD is the state where HEAD points directly at a commit object rather
than indirectly via a branch name. You usually arrive there by running git checkout <commit-hash>, git checkout <tag>, or git checkout origin/main, anything that
asks Git to put you at a specific point in history that is not a local branch. Git
will print a slightly alarming warning telling you so. You can look around, run
tests, even make commits, but those new commits are not attached to any branch and
will be unreferenced (eventually garbage-collected) the moment you switch away.
To get out: if you do not want to keep any new commits you made, git switch main
(or whichever branch) puts you back. If you do want to keep new commits, create a
branch first with git switch -c new-branch-name. If you have already switched away
and now want a lost commit back, git reflog will still show the hash for up to
90 days, and git switch -c rescue <hash> rebuilds a branch on it.