Skip to content
Foxipa

Suggest an edit

Tell us what should be corrected, clarified, or improved. Submission will be connected to Foxipa’s contribution service later.

No information is sent anywhere yet. Your suggestion is only prepared in your browser.

Learning

Git Detached HEAD: Inspecting Commits Without Moving a Branch

Understand detached HEAD as a direct commit reference, inspect its state precisely, observe detached commits without moving a named branch, and reattach safely to a branch.

Levels: Intermediate ~35 min read

Intermediate

Detached HEAD means HEAD names a commit directly

V434 established the normal attached model: HEAD is a symbolic reference to the current branch, and that branch ref resolves to its tip commit. Detached HEAD is the other representation: HEAD directly references a commit instead of symbolically naming a branch. The commit can be an arbitrary point in history; it does not have to be the tip of a branch.

bash
git switch --detach HEAD~1
git rev-parse HEAD
git rev-parse --symbolic-full-name HEAD
git rev-parse --abbrev-ref HEAD

Quick check

What changes when HEAD becomes detached?

HEAD is attached to refs/heads/main.HEAD now resolves directly to the selected commit; no branch name is the symbolic target.

Use git switch --detach for deliberate inspection or experiments

The official `git switch` command provides `--detach` specifically for switching to a commit for inspection or discardable experiments. Its start point may be a commit expression or another revision that resolves to a commit. Unlike ordinary `git switch <branch>`, the operation does not make a named branch current.

bash
git switch --detach HEAD~1
git status --short --branch
git rev-parse HEAD

Quick check

Which command deliberately switches to a commit without attaching HEAD to a branch?

main is currently attached and HEAD~1 names an earlier commit.The worktree is at that commit and HEAD is detached.

Detaching at a branch tip does not attach HEAD

A subtle but important boundary is that detachment is about the form of HEAD, not whether the selected commit happens to equal a branch tip. `git switch --detach main` can select exactly the commit named by `main`, but HEAD is still detached. The branch ref remains a separate name pointing at that same commit.

bash
git switch --detach main
git rev-parse HEAD^{commit}
git rev-parse main^{commit}
git rev-parse --symbolic-full-name HEAD

Quick check

If `git switch --detach main` selects the exact commit currently named by main, what is true?

main names commit X and the learner explicitly requests --detach main.HEAD directly names X while main independently continues to name X.

Inspect detached state with reference-aware commands

Detached state should be established from Git’s observable reference behavior. `git symbolic-ref HEAD` fails because HEAD is no longer a symbolic ref. `git rev-parse --symbolic-full-name HEAD` succeeds and reports `HEAD`, reflecting the direct reference form. `git rev-parse --abbrev-ref HEAD` likewise reports `HEAD`, while `git status --short --branch` reports `## HEAD (no branch)` in the tested Git environment.

bash
git symbolic-ref HEAD
git rev-parse --symbolic-full-name HEAD
git rev-parse --abbrev-ref HEAD
git status --short --branch

Quick check

What should you expect from `git symbolic-ref HEAD` while HEAD is detached?

HEAD directly references a commit.The command returns a non-zero status rather than a branch ref name.

Quick check

What does `git rev-parse --symbolic-full-name HEAD` report in detached state in the tested Git version?

HEAD is detached at commit X.The command prints `HEAD` with a successful exit status.

A commit made while detached advances HEAD, not a branch

Detached HEAD does not prohibit commits. Git’s glossary explicitly notes that commands such as `git commit` still work: the new commit becomes the tip of the history represented by HEAD, while no branch ref is moved. This makes detached commits useful for experiments, but a learner should verify which named refs still point to the earlier history before switching away.

bash
git rev-parse main
git rev-parse HEAD
git commit --allow-empty -m "detached experiment"
git rev-parse HEAD
git rev-parse main

Quick check

After an ordinary commit while HEAD is detached, which ref moves?

HEAD and main both resolve to commit X; HEAD is detached.The new commit is reached by HEAD, while main still resolves to X.

Returning to a branch reattaches HEAD

To leave detached state and resume ordinary branch-based work, switch to a named branch. The branch ref itself is not changed merely by reattaching. HEAD becomes a symbolic reference to that branch again, and subsequent normal commits advance that branch tip.

bash
git switch main
git symbolic-ref HEAD
git rev-parse --symbolic-full-name HEAD
git branch --show-current

Quick check

What does `git switch main` do when HEAD is detached and main exists?

HEAD is detached and main is a named local branch.HEAD symbolically targets refs/heads/main again.

Detached HEAD is not the same as an unborn branch

V434 covered the unborn-branch case: HEAD can symbolically name `refs/heads/main` before that branch has its first commit. Detached HEAD is different because HEAD is not symbolic at all; it directly names a commit. The two states can both have no usable branch tip for different reasons, so the reference form is the reliable distinction.

bash
git symbolic-ref HEAD
# attached unborn repository: refs/heads/main

git switch --detach HEAD
# detached only after a commit exists
git symbolic-ref HEAD

Quick check

Which observation distinguishes an unborn attached HEAD from detached HEAD?

The repository has no first commit in the unborn case, or a commit is selected in the detached case.Inspecting the reference form distinguishes the states without relying on prompt text.

Practice what you learned

Use the dedicated Git code-completion exercise for this lesson.

Official-source boundary

This lesson uses only official Git documentation: `git-switch` for `--detach` and switching semantics, the Git glossary for the definition and commit behavior of detached HEAD, and `git-rev-parse` / `git-symbolic-ref` for reference inspection. The current official `git-switch` manual is documented as unchanged in Git 2.56.0 after its 2.55.0 update. Behavioral assertions in this lesson were executed with local Git 2.47.3, so version-sensitive observations are reported as observations rather than silently generalized beyond the tested version.

Was this page helpful?

Mark this learning resource complete to track your learning progress.