Part II: Chapter 2.6

HEAD

The "You Are Here" Pointer

If you have 10 branches in your repository, how does Git know which one you are currently looking at? It uses a special pointer called HEAD. Notice it is fully capitalized—it is a single, unique file at the root of the `.git` directory.

A Pointer to a Pointer

Unlike branches (which point directly to a Commit Hash), the HEAD file usually points to a Branch. It says exactly: "I am currently standing on branch X".

When you type git checkout feature-branch, Git literally just rewrites the text inside the .git/HEAD file to say ref: refs/heads/feature-branch. That is the entire operation of changing branches.

.git/HEAD
ref: refs/heads/main
1a2bInitial
main
HEAD
3c4dSetup DB
5e6fAdd Auth
feature
7g8hCSS Fixes

Detached HEAD

You may have seen the terrifying "Detached HEAD" warning in Git. What does it actually mean?

Normally, HEAD points to a named branch (like `main`). If you make a new commit, Git moves the branch forward, and HEAD stays pointing to the branch. But if you check out a specific Commit Hash directly (e.g. git checkout 9f8a357), Git has to write that hash directly into the HEAD file instead of writing a branch name. That's it! "Detached HEAD" just means the HEAD file contains a raw hash instead of a branch reference.

Why is it dangerous?

If you are in a Detached HEAD state and make a new Commit, Git creates the Commit... but no branch is pointing to it! If you checkout another branch, that new Commit becomes "abandoned" (unreachable by any bookmark) and will eventually be permanently deleted by Git's garbage collection.

Proving the Architecture

Let's view the literal contents of the HEAD file on a normal branch, and then see what happens when we detach it.

Terminal
$cat .git/HEAD
ref: refs/heads/main
$git checkout 9f8a357
Note: switching to '9f8a357'.
You are in 'detached HEAD' state.
$cat .git/HEAD
9f8a357851f9eafaa9430c5e7b2f6efb35785199