Skip to content
WorktreeWise
#git#troubleshooting#detached-head#branches

Detached HEAD in worktree

W

WorktreeWise Engineering Team

Updated Sep 2026•5 min read
⚡

TL;DR: The 30-Second Summary

When a worktree is created from a tag, commit SHA, or remote branch without `-b`, it enters 'detached HEAD' state. Any commits made here are not bound to a branch and will be lost on cleanup. Fix this by creating a branch pointer immediately with `git switch -c <new-branch>`.

The Real-Life Scenario

“Sam creates a worktree to test release tag `v1.4.0`. While testing, he writes two critical bugfixes and commits them. Only later does he notice the warning: `You are in 'detached HEAD' state. Commits made here do not belong to any branch`.”

Detached HEAD is one of the most common surprises in Git worktrees. When you create a worktree from a tag or commit hash instead of a local branch, Git checks out the exact commit directly. While useful for read-only inspection, committing in this state risks losing your work during garbage collection.

The Danger of Detached HEAD

When `HEAD` is detached, commits are not referenced by any branch pointer in `refs/heads/`. If you delete the worktree or switch commits, the new commits become orphaned and will eventually be permanently deleted by `git prune` and `git gc`.

What you see in the terminal:

terminal output
$ git -C ../test-wt status
HEAD detached at v1.4.0
nothing to commit, working tree clean
🔍

Under the Hood: Git Plumbing & Architecture

In a normal branch checkout, `.git/worktrees/<name>/HEAD` contains `ref: refs/heads/<branch>`. In a detached worktree, `HEAD` contains a raw 40-character commit SHA directly. Any new commit updates `HEAD` to the new SHA, but no ref in `refs/heads/` is updated.

Quick Command Recipes

Copy and adapt these commands directly in your terminal:

Attach commits to a new branch immediately

Creates a new branch pointing to your current detached commit and attaches HEAD to it.

bash
git switch -c fix/release-bugfix

Check current commit SHA

Prints the exact commit hash so you have a safety record before switching.

bash
git rev-parse HEAD
TUTORIAL

Step-by-Step Practical Walkthrough

Follow these verified steps to safely resolve the issue and guarantee that your filesystem and Git references are in sync.

1

Check your current detached commits

Inspect the commit log in the worktree to verify what commits were created while detached.

terminal
git log --oneline -n 3
Expected Output:
a93b41c (HEAD) fix: correct calculation on billing vat
412e8b0 fix: handle empty response payload
e104a99 (tag: v1.4.0) release version 1.4.0
2

Create and switch to a new branch

Turn the detached state into a proper named branch instantly.

terminal
git switch -c fix/release-bugfix
Expected Output:
Switched to a new branch 'fix/release-bugfix'
3

Verify the worktree is now bound to the branch

Run status to confirm HEAD is now tracking your new branch.

terminal
git status
Expected Output:
On branch fix/release-bugfix
nothing to commit, working tree clean

Edge Cases & Advanced Scenarios

You already switched away and lost the commit SHA

Use `git reflog` inside the worktree or main repo to recover the orphaned commit SHA.

bash
git reflog -n 5
git branch recover-work <sha>
⚠️

Common Mistakes to Avoid

❌ Mistake: Removing the worktree before creating a branch

Why it causes trouble: The commits become unreferenced dangling objects eligible for garbage collection.

What to do instead: Always run `git switch -c <name>` before deleting a detached worktree.

✓

Verification Checklist

  • ✓`git status` reports 'On branch <branch_name>'
  • ✓`git worktree list` displays `[branch-name]` instead of `(detached HEAD)`
💡

Senior Engineering Tips

  • ★In WorktreeWise, when creating a worktree from a tag or commit, the UI automatically prompts you to create a new branch or explicitly check out as detached.

Key Takeaways

  • 01.Detached HEAD means your commits are not anchored by any named branch.
  • 02.`git switch -c <new-branch>` saves detached commits in one step.
  • 03.Use `git reflog` if you accidentally navigated away before saving.
TOOL

How WorktreeWise Solves This Visually

WorktreeWise eliminates the manual friction and mental overhead of CLI flags. It displays real-time branch states, uncommitted modifications, active terminals, and lock statuses across all worktrees on a single visual dashboard.

WorktreeWise interface creating a worktree from a release tag with branch options
WorktreeWise interface creating a worktree from a release tag with branch options
W

WorktreeWise Engineering Publication

Written and curated by the core WorktreeWise team. We build developer tools that turn Git worktrees, workflows, and parallel AI coding agents into second nature.

Related Troubleshooting Guides

View all troubleshooting →