What does detached HEAD mean in Git, and how can I keep new work?
Quick answer
Normally, HEAD refers to a branch, which points to a commit. If you check out a commit or tag directly, HEAD points to that commit instead: Git calls this detached HEAD. Commits you make there are not added to a named branch and may become hard to find after you move away. If the work matters, create a branch at the current commit with git switch -c keep-work.[S1]
The question, the details, the next step.
A practical checklist
Identify the commit containing the work you want to preserve.
If it is current, run
git switch -c keep-workbefore moving away.Choose a branch name that helps you recognize the work later.
If you already moved away, consider the reflog as a possible way to locate the commit.
Do not assume recovery is guaranteed; identify the intended commit before acting on it.
A few more useful answers
Does detached HEAD mean my commit is already lost?
No. It means HEAD points directly to a commit rather than a named branch. A commit made there is not added to a branch and may become hard to find after you move away.[S1]
What does `git switch -c keep-work` preserve?
It creates a branch at the current commit, giving that commit a named reference. Run it while the commit you want to keep is current.[S1]
Can the reflog always recover a detached commit?
The Git manual describes using the reflog to recover a commit’s object name, but does not establish that every commit is recoverable. Treat it as a possible recovery path, not a guarantee.[S1]
Sources
References used for this guide.
- [S1] Git git-checkout documentation: detached HEAD git-scm.com · Checked
