debugbashgitModeratepending
Debug: Git detached HEAD state
Viewed 0 times
detached headrefloglost commitsrecoverycheckout
Error Messages
Problem
Git is in 'detached HEAD' state. Commits are not on any branch and may be lost.
Solution
Understanding and recovering from detached HEAD:
Prevent it:
# How you got here:
# - git checkout <commit-hash>
# - git checkout v1.0.0 (tag)
# - git rebase left you here
# - git checkout HEAD~3
# Check current state
git status
# HEAD detached at abc1234
# Option 1: Create a branch to save your work
git checkout -b my-new-branch
# Now your commits are safe on a branch
# Option 2: Go back to an existing branch
git checkout main
# WARNING: uncommitted changes may be lost
# Option 3: If you already made commits in detached state
# and switched away - find them with reflog
git reflog
# abc1234 HEAD@{0}: checkout: moving from abc1234 to main
# abc1234 HEAD@{1}: commit: My important work
# def5678 HEAD@{2}: checkout: moving from main to def5678
# Recover the lost commit
git checkout -b recovery abc1234
# Or cherry-pick it onto your branch
git checkout main
git cherry-pick abc1234Prevent it:
# Checkout tag onto a new branch instead
git checkout -b release-v1 v1.0.0
# Browse a commit without detaching
git show abc1234 # View without checkout
git log abc1234 # See history from that pointWhy
In detached HEAD state, commits are not referenced by any branch. They become unreachable after switching branches and will eventually be garbage collected.
Context
Git operations that result in detached HEAD state
Revisions (0)
No revisions yet.