Ch. 16 · Git & CI/CD

Git Detached HEAD

What a detached HEAD is, when it happens, and how to keep commits made in that state.

~2 min readbeginnerupdated Oct 5, 2026

The HEAD normally points at a branch, which points at a commit. In a detached HEAD, HEAD points directly at a commit instead of a branch, which happens when you check out a tag, a specific commit or a remote branch. New commits are then not on any branch.

Before you start

You should be comfortable with commits and branches. This article covers the detached state and how to recover.

Step-by-step walkthrough

Step 1: Recognize the state

Checking out a commit hash, a tag or origin/main detaches HEAD, and Git prints a warning. You can inspect and build in this state safely; nothing is wrong until you commit.

Step 2: Commits here are unreferenced

A commit made in a detached HEAD is reachable only by HEAD, not by a branch. Switching branches leaves those commits unreferenced, so they can eventually be garbage-collected. They are not lost immediately, but they are not protected.

Step 3: Create a branch to keep them

Before leaving, create a branch at the current commit: git switch -c keep-work (or git branch keep-work). That gives the commits a branch to live on. If you already left, the reflog still points at them, and you can create a branch from the reflog entry.

Worked scenario

The commit is kept by branching before leaving.

git switch --detach v1.2.3     # detached HEAD at the tag
git switch -c fix-on-v1.2.3    # attach the work to a new branch
Terminal

Walk through the example

Detaching lets you start from the tagged commit. Committing there produces a commit not on any branch, so git switch -c immediately creates a branch pointing at it. Now the work is safe and can be pushed or merged normally.

Common mistake

Making commits in a detached HEAD and then switching branches, leaving the commits unreferenced, which feels like losing work. Another is panicking: the reflog retains the commits for a while, so recovery is usually possible.

Verify the behavior

Detach at a commit, make a commit, and confirm git branch does not list it. Switch away and confirm the commit is unreferenced, then recover it from git reflog and create a branch.

Interview exercise

Is a detached HEAD an error?

Answer and reasoning

No. It is a normal state for inspecting a specific commit, tag or remote branch. It becomes a problem only if you commit and then leave without creating a branch, because the commits are not on a branch. In that case the reflog still references them, so you can create a branch and recover.

Continue learning

Compare recovery in Reflog recovery and history in Revert vs reset. Read the Git detached HEAD documentation and try the Git interview questions.

More in Git & CI/CD

read ✓Git & CI/CD · mid

Git Hooks and Local Automation

Automate checks with client and server hooks, share them through a framework, and enforce the same rules in CI where they cannot be bypassed.

~2 min readread →
read ✓Git & CI/CD · hard

Git LFS for Large Files

Track large binaries with Git LFS, keep pointer files in the repository, and plan for LFS storage and access.

~2 min readread →
read ✓Git & CI/CD · hard

Git Partial and Shallow Clones

Fetch less history and content with shallow and partial clones, and understand what you give up.

~2 min readread →
esc