Skip to main content
AllDevToolsHub
Back to error codes
ERROR detached HEAD

Git Detached HEAD State

Git Error

HEAD points directly to a commit instead of a branch, meaning new commits will not be on any branch.

Root Cause

Checking out a specific commit hash, tag, or remote branch directly instead of a local branch.

How to Fix

Create a new branch from the current position: git checkout -b new-branch-name. Or return to a branch: git checkout main.

Quick Summary

Detached HEAD means you are not on a branch, commits made here will be lost when you switch branches. Create a new branch to save your work: git checkout -b new-branch.

Key Takeaways

Key Takeaways

  • HEAD points to a commit, not a branch
  • New commits are not on any branch and will be garbage collected
  • Create a branch to save work: git checkout -b new-branch
  • Return to a branch: git checkout main
Use Cases

When to use it

  • Checking out a specific commit to inspect it
  • Checking out a tag
  • Accidentally checking out a remote branch directly
Watch out

Common Mistakes

  • Making commits in detached HEAD state without creating a branch, the commits will be lost
FAQ

detached HEAD Git Detached HEAD State, Frequently Asked

How do I recover commits made in detached HEAD state?

git reflog shows all recent HEAD positions. Find the commit hash and create a branch: git checkout -b recovery-branch <commit-hash>

Still having issues?

Check your network logs or use our developer tools to inspect headers, decode tokens, or validate your requests.