| Tested on | RHEL 10.2 (Coughlan) |
|---|---|
| Package | git 2.52.0 |
| Applies to | Any host with Git installed |
| Privilege | Normal user |
| Scope | Recognize detached HEAD, enter it safely to inspect history, save work with a new branch, return to an existing branch, and recover orphaned commits with reflog. Not a full switch, checkout, or reflog tutorial. |
| Related guides | Git switch command Git list branches Git reflog tutorial Revert to previous commit Git branch examples |
HEAD is Git’s way of tracking where you currently are. Normally it follows a branch such as main; in detached HEAD, it points directly to a commit instead. That is normal when you inspect an old snapshot — but new commits you make there are not on any branch until you attach them.
Four situations this article covers:
- recognize detached HEAD;
- inspect an old commit without moving a branch;
- save commits you made while detached;
- recover commits after you switched away too soon.
Pick your goal in the table below, then jump to that section.
Attached HEAD vs Detached HEAD
| State | What HEAD points to |
Where new commits go |
|---|---|---|
| Attached (normal) | A branch name such as main |
That branch |
| Detached | A commit hash directly | New commits are created, but no branch name points to them until you create one |
Attached flow:
HEAD → main → latest commitDetached flow:
HEAD → specific commit (no branch name)Detached HEAD is useful for inspection. Treat it as temporary when you start committing — attach a branch with git switch -c or switch back before you forget.
Choose the Right Fix
| Your goal | Command |
|---|---|
| Confirm you are detached | git status -sb or empty git branch --show-current |
| Inspect an old commit without moving a branch | git switch --detach <commit> |
| Return to the branch you were on before | git switch - |
| Return to a named branch | git switch main |
| Keep commits made while detached | git switch -c new-branch |
| Recover a commit you left behind | git reflog, then git branch name <hash> |
For everyday branch switching, see Git switch command. For listing branches and the * marker, see Git list branches.
Recognize Detached HEAD
Scenario: Git printed You are in 'detached HEAD' state and you want to confirm what that means on your repo.
Short status shows you are not on a branch:
git status -sb## HEAD (no branch)The header HEAD (no branch) means no branch name is attached — only a commit hash.
git branch --show-current is built for printing the current branch name:
git branch --show-currentWhen you are detached, it prints nothing — not the word HEAD. That empty output is another sign you are not on a branch.
In normal work on main, status shows the branch on the first line:
## mainAnd git branch --show-current prints the branch name alone:
git branch --show-currentmainHow You Enter Detached HEAD
Detached HEAD is not a failure state. Git enters it whenever HEAD points at a commit without going through a local branch name.
Common causes:
git switch --detach <commit>orgit switch --detach HEAD~1git checkout <commit>— older Git workflows commonly use this to enter detached HEAD- checking out a tag such as
git switch --detach v2.0 - checking out a remote-tracking ref directly, such as
git switch --detach origin/main
Each command moves your working tree to that commit. Until you create a branch or switch back, new commits are not part of main or any other branch line.
Inspect an Old Commit Without Changing a Branch
Scenario: you want to read files or run tests at an older commit without moving main.
Start from a repo whose tip is commit 7b5f594 v3. Move back one commit in detached mode:
git switch --detach HEAD~1Git lands on the older commit and detaches HEAD:
HEAD is now at 7f0c441 v2Confirm you are not on a branch:
git status -sb## HEAD (no branch)Browse the tree at v2. When you are done and have not made new commits, return to where you were:
git switch -Previous HEAD position was 7f0c441 v2
Switched to branch 'main'Confirm you are back on the branch:
git status -sb## mainYou are back on main with no extra branch created.
Save Work With a New Branch
Scenario: you made one or more commits while detached and want to keep them.
If you already made commits while detached, create a branch at your current commit before switching away:
git switch -c save-from-detachedSwitched to a new branch 'save-from-detached'The new branch now points to your detached commit, so that commit is reachable by a normal branch name and will not be left behind when you switch elsewhere. Verify with branch list:
git branchmain
* save-from-detachedFrom here you can merge, push, or keep working on a normal branch. Push the new branch with git push -u origin save-from-detached if you want to publish it — see Git branch examples for more branch workflows.
Return to a Branch Without Saving
Scenario: you experimented in detached HEAD and do not need the commits you made there.
After a detached commit, switching to main without creating a branch can produce a warning like this:
git switch mainSample output when Git would orphan the detached commit:
Warning: you are leaving 1 commit behind, not connected to
any of your branches:
6ba3a0f orphan detached commit
If you want to keep it by creating a new branch, this may be a good time
to do so with:
git branch <new-branch-name> 6ba3a0f
Switched to branch 'main'If you intended to discard that work, the switch to main is fine — the commit is no longer referenced by any branch name. Git may still remember it temporarily in the reflog, which the next section uses for recovery.
If you made no new commits while detached, git switch main or git switch - simply reattaches HEAD to the branch with no orphan warning.
Recover a Commit With Reflog
Scenario: you left detached HEAD without creating a branch and now need commit 6ba3a0f back.
Reflog records recent HEAD positions, including detached commits:
git reflog -37b5f594 HEAD@{0}: checkout: moving from 6ba3a0f to main
6ba3a0f HEAD@{1}: commit: orphan detached commit
7f0c441 HEAD@{2}: checkout: moving from main to HEAD~1In this example, 6ba3a0f at HEAD@{1} is the detached commit you left behind. Create a branch pointing at it:
git branch recover-work 6ba3a0fSwitch to that branch to continue working:
git switch recover-workSwitched to branch 'recover-work'The recovered branch tip should include that detached commit:
git log --oneline -36ba3a0f orphan detached commit
7f0c441 v2
c926a29 v1Reflog entries expire eventually. For a full recovery walkthrough, see Git reflog tutorial.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
git branch --show-current prints nothing |
Detached HEAD | Confirm with git status -sb; git switch main or git switch -c to attach |
Commits missing after git switch main |
Left detached commits without a branch | Find hash in git reflog; git branch name <hash> |
Normal git push has no current branch to publish |
Detached HEAD | git switch -c branch-name, then push that branch |
| Warning about leaving commit behind | Switching off detached tip with unpushed commit | git switch -c name before switching, or note hash from warning for recovery |
| Cannot find the detached commit after switching away | Reflog expired or GC ran | Recovery may be impossible — create branches before switching away |
References
- Git switch documentation
- Git checkout documentation
- Git branch documentation
- Git reflog documentation
Summary
Detached HEAD means HEAD sits on a commit, not a branch. You see HEAD (no branch) in git status -sb, and git branch --show-current prints nothing. That happens when you inspect an old commit, a tag, or a remote ref with git switch --detach.
For inspection without moving main, detach, look around, then git switch - or git switch main. When you commit while detached, run git switch -c before leaving so a branch name points to those commits. If Git warns that you are leaving commits behind, either create that branch from the hash in the message or recover later from git reflog.
Detached HEAD is a tool for exploring history, not a broken repo — as long as you attach a branch before you switch away and leave those commits without a branch name pointing to them. Branch creation, merging, and everyday switching live in Git branch examples and Git switch command.

