Skip to content
rajin

14HEAD and detached HEAD

core25–30 minHandbook pp. 33–34 Not started

Warm-up · from “Commit graphs and movable references”

Does creating a branch duplicate history?

No notes: write what you remember, then check. Recalling before reading is what makes it stick.

The problem

You check out an old version to reproduce a bug, commit a fix, then switch back to main. Where did the new commit go?

After this lesson you can

  • Distinguish attached and detached HEAD.

  • Create a branch from detached work.

  • Explain reachability and reflog recovery.

Key terms

Full glossary

Say what each term means, then open it to check.

  • HEAD · new in this lesson

    Current symbolic branch reference or detached commit identity.

  • Commit · from lesson 7

    Object identifying a root tree, parent(s), metadata and message.

  • Ref · from lesson 3

    Name identifying an object; branch refs usually identify commits.

  • Reachability · preview, covered in lesson 27

    Whether an object can be reached by traversing links from roots.

  • Reflog · preview, covered in lesson 28

    Local log of ref updates when enabled.

Visual mental model

  1. AttachedHEAD → main → B
  2. DetachedHEAD → A

Build the mental model

Normally HEAD is a symbolic ref to the current branch, such as refs/heads/main. A new commit advances that branch. In detached mode HEAD identifies a commit directly, so a new commit advances HEAD but does not advance any branch. Detachment is useful for inspecting historical states and testing releases.

SimulationDetach, commit, rescue

A simplified model for building intuition. It does not read your sandbox; use the terminal for real behaviour.

Under the hood

If you make valuable commits while detached, create a branch before switching away, using git switch -c NAME. If you already switched away, inspect git reflog and create a branch at the old commit. Commits may still be reachable through another ref or a reflog, but recovery is not guaranteed forever. git symbolic-ref -q HEAD succeeds only for symbolic HEAD; git rev-parse HEAD resolves the current commit in either mode.

Common mistake · important nuance

Detached HEAD is not an error. Losing the convenient reference to new work is the risk.

Move between parts freely. Completion counts only what you verify and answer.