---
title: "You are in 'detached HEAD' state: what does it mean and how do I get out?"
description: "Detached HEAD means you are on a commit, not a branch. Learn how you got there, why new commits can go missing, and how to keep them safely with git switch -c."
url: https://www.danny.engineering/answer/git/you-are-in-detached-head-state-what-does-it-mean-and-how-do-i-get-out
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Git"]
---

# You are in 'detached HEAD' state: what does it mean and how do I get out?

## The short answer


You have checked out a specific commit instead of a branch. Looking around is harmless. If you have not made any commits, switch back to a branch. If you have made commits you want to keep, create a branch where you are first.


```bash
# no new commits: go back to your branch
git switch main

# made commits you want to keep: give them a branch
git switch -c my-fix
```


## Why it happens


`HEAD` is Git's pointer to what you currently have checked out. Normally it points at a branch name, and the branch points at a commit. When you commit, the branch moves forward and `HEAD` moves with it.


In detached HEAD state, `HEAD` points straight at a commit, with no branch in between. Git shows a message like this:


```bash
Note: switching to 'a81b4de'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
```


You can still commit, but no branch records those commits. As soon as you switch away, nothing points at them, and they become hard to find.


You usually get here by checking out something that is not a local branch:

- A commit hash, for example `git checkout a81b4de`.
- A tag, for example `git checkout v2.1.0`.
- A remote-tracking branch, for example `git checkout origin/main`.
- `git switch --detach`, on purpose.
- In the middle of a rebase or a `git bisect`, where Git detaches `HEAD` while it works.
- Inside a submodule, which checks out a fixed commit by default.
- In many CI systems, which check out the exact commit being built.

To confirm, run `git status`. It will say something like "HEAD detached at a81b4de".


## How to fix it


### Keep your commits with git switch -c


This is the most important step if you have committed anything. It creates a new branch at the current commit and attaches `HEAD` to it.


```bash
git switch -c my-fix
# older equivalent: git checkout -b my-fix
```


You can then merge it into another branch, or push it and open a pull request:


```bash
git switch main
git merge my-fix
```


`git switch` has been available since Git 2.23. On older versions, use `git checkout -b`.


### Go back to a branch


If you were only looking around, switch back. Any uncommitted edits come with you, or Git stops and tells you to commit or stash them.


```bash
git switch main
# or return to wherever you were before
git switch -
```


### Rescue commits after you already left


When you switch away from commits that are not on any branch, Git warns you and prints their hash:


```bash
Warning: you are leaving 2 commits behind, not connected to
any of your branches:

  a81b4de Add login form validation
  7c0e5b2 Add login form

If you want to keep them by creating a new branch, this may be a good time
to do so with:

 git branch <new-branch-name> a81b4de
```


Copy the hash and create the branch. If the message has scrolled away, find the commit in `git reflog`.


```bash
git branch my-fix a81b4de
```


### Work on a remote branch properly


If you meant to work on a branch that exists on the remote, switch to it by name rather than checking out `origin/main` or similar. Git creates a local branch that tracks the remote one.


```bash
git fetch
git switch feature/login
```


### Finish the operation that detached you


If you are mid-rebase or mid-bisect, do not create branches or switch away. Complete or cancel the operation, and Git reattaches `HEAD` for you.


```bash
git rebase --continue   # or: git rebase --abort
git bisect reset
```


## Common pitfalls

- Committing for an hour in detached HEAD, then switching branches and losing track of the work. Create a branch as soon as you decide to keep anything.
- Trying `git switch origin/main`. Unlike `checkout`, `switch` refuses to detach `HEAD` unless you add `--detach`, and it reports that a branch is expected.
- Assuming detached HEAD is an error. Checking out a tag to test an old release is a normal reason to be detached.
- Pushing from detached HEAD with plain `git push`. There is no current branch to push; create a branch first, or push explicitly with `git push origin HEAD:my-fix`.

## Related reading

- [git-switch documentation](https://git-scm.com/docs/git-switch)
- [git-checkout documentation (see Detached HEAD)](https://git-scm.com/docs/git-checkout#_detached_head)
