What is Detached HEAD

Detached HEAD occurs when HEAD points directly to a commit hash instead of to a branch name. Normally .git/HEAD contains ref: refs/heads/main. In detached state, it contains a raw SHA-1 hash.

How it works

You enter detached HEAD by checking out a commit, tag, or remote-tracking branch directly:

git checkout a1b2c3     # detached at commit
git checkout v1.0       # detached at tag
git checkout origin/main  # detached at remote branch

You can make commits in detached HEAD, but they are not on any branch. When you switch to a branch, those commits become unreachable — no ref points to them. They will be garbage collected after the reflog entry expires.

To save work from detached HEAD:

git checkout -b new-branch   # creates a branch at the current commit

To return to a branch:

git checkout main

Why it matters

Detached HEAD is not dangerous if you understand it. It is useful for inspecting old commits, running tests against a specific version, or experimenting. The risk is making commits that you forget to save to a branch. Git warns you when entering detached HEAD state for exactly this reason.

See How Git Branching Works for the full explanation of HEAD, refs, and detached state.