HEAD and a Detached Checkout

HEAD normally identifies the active branch. A detached checkout can make it point directly to a commit for inspection.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace two checkouts
  2. Name work you intend to keep
  3. Record where a new commit is reachable
  4. Inspect state before changing branches

Trace two checkouts

Opening a named branch lets new commits advance that branch. Opening a particular commit directly can leave HEAD detached. Either can show the same files initially. Branches as references explain which name would move afterward.

Name work you intend to keep

A detached commit can exist without advancing a named branch. If work made there must be retained, attach it to a branch before switching away. Commit parent links still record the commit's ancestry.[1]

Record where a new commit is reachable

When HEAD refers to branch topic, commits advance that branch. Checking out an older commit directly can make HEAD point to that commit instead. A new commit there can exist without any branch being advanced. Give retained work a deliberate reference before switching away. See commits and their parents.

Attached: HEAD→topic→B
Detached at A: HEAD→A
Commit C: HEAD→C, C parent A, topic still points to B

Inspect state before changing branches

Detached checkout is useful for builds tied to a specific commit. It is also easy to forget how to find new work from it. Verify current HEAD mode and working changes before leaving the state.

References

  1. Scott Chacon and Ben Straub, Pro Git ↩