What is a detached HEAD state in Git?
Quick Answer
Normally HEAD points at a branch, which points at a commit. In detached HEAD, HEAD points directly at a commit (because you checked out a specific SHA, a tag, or origin/main). You can look around and even commit, but new commits aren't on any branch and can be garbage-collected once you move away.
Usually HEAD points to a branch and the branch points to a commit. A detached HEAD is when HEAD points straight at a commit — which happens if I check out a specific hash, a tag, or a remote branch like origin/main directly. I can browse and even commit, but those commits aren't attached to any branch, so if I switch away, Git can eventually clean them up. If I want to keep work I did there, I create a branch first with git switch -c. If I already lost it, the reflog will still have the commit.
- Common triggers:
git checkout <sha>,git checkout v1.2.0, checking out a remote branch by itsorigin/name. - To keep work done in detached HEAD:
git switch -c new-branch(orgit branch new-branch) before switching away. - If you already left:
git reflogshows the commit you can branch from.