You are in 'detached HEAD' state.
You checked out a specific commit (not a branch). Any commits you make won’t belong to any branch and could be lost.
What happened
# These put you in detached HEAD:
git checkout abc1234 # Checkout a commit hash
git checkout v1.0.0 # Checkout a tag
git checkout origin/main # Checkout a remote branch directly
You’re looking at a specific point in history, not the tip of a branch.
Fix 1: Just Want to Go Back to a Branch
If you didn’t make any changes:
git checkout main
# or
git switch main
Done. You’re back on a branch.
Fix 2: Save Commits You Made in Detached HEAD
If you made commits while detached and want to keep them:
# Create a new branch from where you are
git checkout -b my-saved-work
# Now your commits are safe on this branch
# Merge into main if you want
git checkout main
git merge my-saved-work
Fix 3: Already Left Detached HEAD and Lost Commits
If you already switched branches and your detached commits seem gone:
# Find the lost commit
git reflog
# Output shows recent HEAD positions:
# abc1234 HEAD@{0}: checkout: moving from abc1234 to main
# def5678 HEAD@{1}: commit: my important work ← this one!
# Recover it
git checkout -b recovered def5678
Git keeps commits for ~30 days even if no branch points to them. reflog is your safety net.
Prevention
# Use switch instead of checkout (clearer intent)
git switch main # Switch to branch
git switch -c new-branch # Create and switch
# If you want to look at an old commit, create a branch
git checkout -b explore-old-code abc1234