[Fixed] You are in ‘detached HEAD’ state in Git: Step-by-Step Troubleshooting Guide

Executive Summary & Root Cause Analysis: The warning You are in 'detached HEAD' state occurs when your repository’s HEAD pointer points directly to a specific commit SHA hash rather than a named branch reference (such as main or feature/auth). While you can freely inspect files, run builds, and even make experimental commits in this state, any new commits created while detached are not anchored to a branch and can eventually be garbage-collected by Git if you switch away. Common root causes include: (1) checking out a specific commit hash (git checkout <commit-sha>), (2) checking out a release tag (git checkout v1.2.0), (3) checking out a remote-tracking branch directly (git checkout origin/main instead of git switch main), and (4) Git submodules or CI/CD runners checking out pinned commit hashes.

Step 1: Quick Fix – Exit Detached HEAD When You Have No New Commits

If you only checked out an older commit or tag to inspect code or test a build and did not create any new commits, simply switch back to your previous branch:

# 1. Check current status and confirm no uncommitted work or detached commits exist
git status

# 2. Switch back to your previous branch (shortcut)
git switch -

# Or explicitly switch to your main branch:
git switch main

If you entered detached HEAD by running git checkout origin/main, use git switch --track origin/main (or git checkout -b main origin/main) to create a proper local tracking branch.

Step 2: Preserve Commits Made While in Detached HEAD State

If you already made one or more commits while in detached HEAD and want to keep them, create a new branch right at your current HEAD before switching away:

# 1. Create and switch to a new branch anchored at your current detached commits
git switch -c <new-branch-name>

# (Legacy Git equivalent):
# git checkout -b <new-branch-name>

If you want those detached commits merged directly into an existing branch (such as main), create a temporary branch first, switch to main, and merge:

git switch -c temp-save
git switch main
git merge temp-save
git branch -d temp-save

Step 3: Alternative Workarounds – Recover Lost Detached Commits via Reflog

If you accidentally switched back to main while in detached HEAD and noticed your experimental commits vanished, they are still stored in Git’s local reference log (reflog) for at least 30 days:

  1. Inspect the reflog to locate the orphaned commit hash:
    # List recent HEAD movements and commits
    git reflog -n 15
  2. Restore the orphaned commit onto a new branch:
    # Create a recovery branch pointing to the lost commit SHA
    git switch -c recovered-work <commit-sha>

Verification & Testing Steps

Confirm that HEAD is properly attached to a named branch and that all intended commits are present in the branch history:

# Verify current active branch (should NOT say 'HEAD detached')
git status

# Confirm HEAD points to a branch reference (e.g., refs/heads/main)
git symbolic-ref HEAD

# Inspect recent commits and branch pointers
git log --oneline --decorate -n 5

Summary Comparison Table

Scenario What Happened Recommended Command Risk to Work
Inspecting old commit/tag No new commits made git switch main (or git switch -) Zero (Safe)
Committed work while detached New commits exist on unbranched HEAD git switch -c <new-branch> Zero (Anchors commits safely)
Checked out origin/<branch> Missing local tracking branch git switch --track origin/<branch> Zero (Creates tracking branch)
Already switched away & lost commits Detached commits orphaned from branch tree git reflog then git switch -c recover <sha> Low (Recoverable within 30 days)

Leave a Reply