Git Detached HEAD: Fix, Save Work, and Recover Commits

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:

text
HEAD → main → latest commit

Detached flow:

text
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:

bash
git status -sb
output
## 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:

bash
git branch --show-current

When 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:

output
## main

And git branch --show-current prints the branch name alone:

bash
git branch --show-current
output
main

How 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> or git switch --detach HEAD~1
  • git 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:

bash
git switch --detach HEAD~1

Git lands on the older commit and detaches HEAD:

output
HEAD is now at 7f0c441 v2

Confirm you are not on a branch:

bash
git status -sb
output
## HEAD (no branch)

Browse the tree at v2. When you are done and have not made new commits, return to where you were:

bash
git switch -
output
Previous HEAD position was 7f0c441 v2
Switched to branch 'main'

Confirm you are back on the branch:

bash
git status -sb
output
## main

You 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:

bash
git switch -c save-from-detached
output
Switched 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:

bash
git branch
output
main
* save-from-detached

From 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:

bash
git switch main

Sample output when Git would orphan the detached commit:

output
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:

bash
git reflog -3
output
7b5f594 HEAD@{0}: checkout: moving from 6ba3a0f to main
6ba3a0f HEAD@{1}: commit: orphan detached commit
7f0c441 HEAD@{2}: checkout: moving from main to HEAD~1

In this example, 6ba3a0f at HEAD@{1} is the detached commit you left behind. Create a branch pointing at it:

bash
git branch recover-work 6ba3a0f

Switch to that branch to continue working:

bash
git switch recover-work
output
Switched to branch 'recover-work'

The recovered branch tip should include that detached commit:

bash
git log --oneline -3
output
6ba3a0f orphan detached commit
7f0c441 v2
c926a29 v1

Reflog 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


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.


Frequently Asked Questions

1. What is a detached HEAD in Git?

Detached HEAD means HEAD points at a specific commit instead of a branch name. git status -sb shows HEAD (no branch), and git branch --show-current prints nothing.

2. Is detached HEAD dangerous in Git?

It is not an error. It is risky only if you make new commits and switch away without creating a branch to hold them — those commits can become hard to find until reflog recovery.

3. How do I fix detached HEAD in Git?

To keep new commits, run git switch -c branch-name. To return without keeping detached work, run git switch main or git switch - to go back to the previous branch.

4. How do I recover commits from detached HEAD?

Run git reflog, find the commit hash from before you switched away, then git branch recover-name hash and git switch recover-name.

5. Why does Git say you are in detached HEAD state?

You checked out a commit, tag, or remote-tracking ref directly instead of a local branch. HEAD points directly to that commit instead of following a local branch such as main.

6. How do I go back from detached HEAD?

Run git switch - to return to the branch you were on before, or git switch main to jump to a named branch. Create a branch first if you need to keep commits made while detached.

7. Can I push from detached HEAD?

Git expects a branch name for a normal push. Create a local branch with git switch -c, then push that branch to the remote.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.

  • Go (programming language)
  • Python (programming language)
  • DevOps
  • Computer Security
  • Cloud Computing
  • Kubernetes
  • Linux
  • Ansible (software)