Git Detached HEAD Explained (Fix, Recover, Reattach HEAD with Examples)

Git Detached HEAD Explained (Fix, Recover, Reattach HEAD with Examples)

Git Detached HEAD (Quick Fix)

If you see the message "You are in 'detached HEAD' state", it means Git is pointing to a specific commit instead of a branch. This is not an error — but you must act carefully to avoid losing your work.

In short: The safest way to fix detached HEAD is to create a new branch using git switch and continue working.

Below is a quick cheat sheet to handle all common scenarios instantly:

TaskCommand
Go back to previous branchgit switch -
Switch to main/master branchgit switch main
Create new branch (save work safely)git switch -c new-branch
Save changes and continue workgit switch -c feature-branch
Discard changes and exit detached HEADgit switch main
View commit historygit log --oneline
Find lost commitsgit reflog
Recover lost commitgit branch recover <commit-hash>
Checkout specific commit (enter detached HEAD)git checkout <commit>
Checkout previous commitgit checkout HEAD~1
HINT
Always create a new branch using git switch -c <branch> before leaving detached HEAD if you have made changes. Otherwise, your commits may be lost.

What is Detached HEAD in Git?

Detached HEAD in Git explained

The image compares two states side by side:

1. Attached HEAD (Left Side)

  • HEAD is connected to master
  • master points to the latest commit
  • Safe and normal working condition

2. Detached HEAD (Right Side)

  • HEAD points directly to a commit (Commit B)
  • No branch is tracking this commit
  • Any new commits are temporary and can be lost, but can still be recovered using git reflog.

The arrow (git checkout <commit>) shows how you move from attached → detached state.

What HEAD means in Git?

In Git, HEAD is a pointer that tells you where you are currently working in the repository you can inspect commits using git show.

In a normal situation (shown on the left side of the image):

  • HEAD → points to a branch (for example master)
  • The branch → points to the latest commit (Commit C)

So the flow is:

text
HEAD → master → latest commit

This is called an attached HEAD state, and it is the standard way of working in Git.

What is detached HEAD state?

A detached HEAD happens when HEAD points directly to a commit instead of a branch.

From the image (right side):

  • HEAD → directly points to Commit B
  • It is not connected to any branch

This usually happens when you run git checkout:

text
git checkout <commit>

Now your state becomes:

text
HEAD → specific commit (no branch)

That’s why the image clearly shows:

  • ❌ NOT on a branch
  • ⚠️ Commits at risk

If you make new commits here, they are not part of any branch unless you explicitly save them.

Why Git shows “you are in detached HEAD state”

Git shows this warning because:

  • You are not on a branch
  • Any commits you make may be lost later unless saved using a branch created via git switch
  • Your work is not automatically tracked by branch history

Example message:

text
You are in 'detached HEAD' state.

Git also suggests the correct fix:

text
git switch -c new-branch

This creates a new branch and safely attaches HEAD again.

Key takeaway

  • Attached HEAD → normal, safe workflow
  • Detached HEAD → temporary state for testing or exploring commits
  • Always create a branch if you want to keep your work
text
git switch -c my-branch

This ensures your commits are saved and not lost.


When Does Detached HEAD Occur?

Checkout commit (git checkout )

Detached HEAD occurs when you checkout a specific commit instead of a branch using git checkout:

text
git checkout 0c0294d

This moves HEAD directly to that commit, not to any branch.

Checkout tag or remote branch

Detached HEAD also happens when checking out:

  • A tag:
text
git checkout v1.0
  • A remote branch directly:
text
git checkout origin/main

In both cases, you are not on a local branch.

Using git checkout vs git switch

Both commands can cause detached HEAD:

  • Using checkout:
text
git checkout <commit>
  • Using switch:
text
git switch --detach <commit>

👉 Modern Git recommends git switch for clarity.


How to Enter Detached HEAD (Examples)

Checkout a specific commit

Move to an older commit:

text
git log --oneline
git checkout <commit-hash>

Use commit history from git log --oneline (see git show for detailed commit inspection)

Checkout previous commits using HEAD~n

Go back in history:

text
git checkout HEAD~1
git checkout HEAD~2

Each command detaches HEAD.

Checkout tag leading to detached HEAD

Switch to a release/tag:

text
git checkout v2.0

This also puts you in detached HEAD state.


How to Fix Detached HEAD (Step-by-Step)

Fix by switching back to previous branch

Return to your last branch using git switch:

text
git switch -

OR

text
git checkout main

Or use git checkout (older syntax):

Fix by creating a new branch (safe method)

Safest way to keep your changes:

text
git switch -c new-branch

This reattaches HEAD and preserves commits.

Fix using git switch vs git checkout

  • Using switch (recommended):
text
git switch main
git switch -c new-branch
  • Using checkout (older method):
text
git checkout main
git checkout -b new-branch

How to Save Work in Detached HEAD

Save your work before leaving:

text
git switch -c feature-branch

Make changes and commit:

text
git add .
git commit -m "Work from detached HEAD"

Push your new branch using git push:

text
git push -u origin feature-branch

Now your work is safely stored in remote repository.


How to Go Back from Detached HEAD

The quickest way to return to your previous branch is:

text
git switch -

This takes you back to the branch you were working on before entering detached HEAD.

You can explicitly switch back to your main branch:

text
git switch main

OR (older syntax):

text
git checkout master

This reattaches HEAD to a branch and restores normal workflow.

If you have made changes in detached HEAD, do NOT switch immediately.

First save your work:

text
git switch -c my-branch

Then continue working or merge later. This prevents losing commits.


Recover Lost Commits from Detached HEAD

Even if you lost commits, Git keeps a history:

text
git reflog

This shows all recent HEAD movements and commit hashes using git reflog

Once you find the commit (using git reflog):

text
git branch recover <commit-hash>
git switch recover

This restores your lost work.

👉 This is the safest recovery method in Git.


Detached HEAD vs Normal HEAD

Key differences explained

StateHEAD Points ToSafe for WorkCommits Saved
Normal HEADBranchYesYes
Detached HEADCommitNo (temporary)Risk of loss

How HEAD tracks branches vs commits

  • Normal state:
text
HEAD → branch → latest commit
  • Detached state:
text
HEAD → specific commit

In detached HEAD, no branch tracks your work unless you create one.


Common Errors in Detached HEAD

Lost commits after switching branch

If you switch branches without saving:

text
git switch main

Your detached commits may disappear.

Fix:

text
git reflog

Then recover using a new branch.

Cannot push from detached HEAD

If you try:

text
git push

You may get an error because you are not on a branch which is explained in git push examples.

Fix:

text
git switch -c new-branch
git push -u origin new-branch

Frequently Asked Questions

1. What is a detached HEAD in Git?

A detached HEAD state occurs when HEAD points to a specific commit instead of a branch. This usually happens when you checkout a commit, tag, or remote branch directly instead of switching to a local branch.

2. Is detached HEAD dangerous in Git?

No, detached HEAD is not dangerous, but commits made in this state can be lost if you switch branches without saving them. It is mainly used for testing or exploring past commits.

3. How do I fix detached HEAD in Git?

You can fix it by switching back to a branch using git switch branch-name or by creating a new branch using git switch -c new-branch to preserve changes.

4. How do I recover commits from detached HEAD?

Use git reflog to find the commit hash and create a branch using git branch new-branch <commit-hash> to recover lost commits.

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

This message appears when you checkout a commit instead of a branch, meaning HEAD is no longer attached to a branch pointer.

6. How do I go back from detached HEAD?

Use git switch - to return to the previous branch or git switch branch-name to move back to a specific branch.

7. Can I push changes from detached HEAD?

Yes, but you should first create a branch using git switch -c new-branch and then push it to the remote repository.

Summary

Detached HEAD in Git is a temporary state where HEAD points directly to a commit instead of a branch (understanding git branch helps avoid this confusion). It is useful for testing or exploring past commits but can lead to lost work if not handled properly.

To stay safe:

  • Always create a branch before leaving detached HEAD
  • Use git switch -c to save changes
  • Use git reflog to recover lost commits

Understanding this concept helps you work confidently with Git without risking data loss by following a proper Git workflow.


Official Documentation

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade 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.