DEV Community

Cover image for Fix "HEAD detached at": get back to a branch
Jey Singh
Jey Singh

Posted on Originally published at jeysingh.com

Fix "HEAD detached at": get back to a branch

Nothing is broken. You are standing on one commit instead of on a branch. If you only looked around, one command takes you back. If you made commits there, one command keeps them.

The message

You run git status and the first line is not "On branch main". It is this:

HEAD detached at 8eb9869
nothing to commit, working tree clean
Enter fullscreen mode Exit fullscreen mode

Or, if you got there from a tag, you see the tag name instead of a hash: HEAD detached at v1.2.0.

A few words first

  • Commit: a saved snapshot of your project. Every commit has an ID called a hash, a long string like 8eb9869.... Git usually shows only the first 7 characters.
  • Branch: a name that points at one commit, like a bookmark. When you commit on a branch, the bookmark moves forward to your new commit.
  • Tag: a name for one commit that never moves, usually a release like v1.2.0.
  • HEAD: Git's word for "where you are right now". Normally HEAD points at a branch, and the branch points at a commit.
  • Detached HEAD: HEAD points straight at a commit, with no branch in between. You can look, build and even commit, but no bookmark follows you.

How you got here

You did Why HEAD detached
git checkout 8eb9869 (a hash) You asked for a commit, not a branch.
git checkout v1.2.0 or git switch --detach v1.2.0 A tag never moves, so Git cannot put you "on" it.
git checkout origin/main That is a read-only copy of GitHub's branch, not your own branch.
An interactive rebase stopped halfway Git detaches while it replays commits. git status says "rebase in progress".
Clicked an old commit in an editor or GitHub Desktop and chose "checkout" Same as the first row, done for you.

When you check out a commit by hash, Git warns you right then. This is the real text from Git 2.43:

Note: switching to '8eb9869'.

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.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 8eb9869 feat: add cart taxes
Enter fullscreen mode Exit fullscreen mode

Check first (these commands change nothing)

git status
Enter fullscreen mode Exit fullscreen mode

Piece by piece:

  • git: The tool. Every Git command starts with it.
  • status: The action: tell me where I am and what has changed. Read only.

Say it: "Git, where am I and what have I changed?"

If the first line says HEAD detached at, you have not made any commits since you got here. If it says HEAD detached from, you have. That one word tells you which fix you need. If it says interactive rebase in progress, skip to the rebase section below.

git log --oneline -3
0ffc63c fix: hotfix on old release
8eb9869 feat: add cart taxes
d38f7b6 docs: add README
Enter fullscreen mode Exit fullscreen mode

Piece by piece:

  • git: The tool.
  • log: The action: list commits, newest first, starting from where HEAD is. Read only.
  • --oneline: One line per commit: the short hash, then the message.
  • -3: Show only the last 3 commits. Any number works.
  • 0ffc63c fix: ...: The output. The top line is the commit you are on. Copy its hash somewhere safe if you made it yourself.

Say it: "Git, show me the last three commits from where I am, one line each."

Fix 1: you only looked around

No commits made? Go back to your branch. Nothing is lost, because there is nothing to lose.

git switch main
Enter fullscreen mode Exit fullscreen mode

Piece by piece:

  • git: The tool.
  • switch: The action: move to a branch. Your files change to match that branch.
  • main: Which branch. Use your own branch name if it is not main (git branch lists them).

Say it: "Git, put me back on the main branch."

git switch - also works. The - means "wherever I was before", like the back button in a browser.

Fix 2: you made commits and want to keep them

Give your commits a branch to live on, before you switch anywhere else:

git switch -c rescue
Switched to a new branch 'rescue'
Enter fullscreen mode Exit fullscreen mode

Piece by piece:

  • git: The tool.
  • switch: The action: move to a branch.
  • -c: Short for --create. Make the branch first, right here on the commit you are on, then move onto it.
  • rescue: The new branch name. Pick any name that says what the work is, like hotfix-tax.
  • Switched to a new branch 'rescue': What Git prints. git status now says "On branch rescue": you are no longer detached.

Say it: "Git, create a branch called rescue right where I am, and put me on it."

Your commits are now safe. From here you can push the branch and open a pull request, or merge it into main.

Fix 3: you already switched away

If you made commits and then switched to main, Git warned you on the way out:

Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  0ffc63c fix: hotfix on old release

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> 0ffc63c

Switched to branch 'main'
Enter fullscreen mode Exit fullscreen mode

The commit still exists. Put a branch on it using the hash from the warning:

git branch rescue 0ffc63c
Enter fullscreen mode Exit fullscreen mode

Piece by piece:

  • git: The tool.
  • branch: Here, the action is: create a branch. It does not move you; you stay on main.
  • rescue: The new branch name.
  • 0ffc63c: Which commit the branch should point at: the hash Git printed in the warning.

Say it: "Git, make a branch called rescue that points at commit 0ffc63c, but leave me where I am."

Closed the terminal and lost the hash? Git keeps a diary of every place HEAD has been:

git reflog -5
81c5272 HEAD@{0}: checkout: moving from 0ffc63cac003e7e736d2c7bf9258ac9f9fc44f2f to main
0ffc63c HEAD@{1}: commit: fix: hotfix on old release
8eb9869 HEAD@{2}: checkout: moving from main to v1.2.0
Enter fullscreen mode Exit fullscreen mode

Piece by piece:

  • git: The tool.
  • reflog: Short for reference log: a list of every place HEAD has pointed, newest first. Read only. It is kept only on your computer, not on GitHub.
  • -5: Show only the last 5 entries.
  • HEAD@{1}: "Where HEAD was one move ago." Find the line that says commit: with your message, and use its hash in git branch rescue .

Say it: "Git, show me the last five places I have been."

The reflog is the same safety net that saves people after a bad git reset. More on that in git revert vs reset vs restore.

If a rebase is in progress, do not switch

During a rebase, detached HEAD is normal. git branch shows it plainly:

* (no branch, rebasing main)
  main
Enter fullscreen mode Exit fullscreen mode

Finish the rebase, or cancel it and put everything back the way it was:

git rebase --abort
Enter fullscreen mode Exit fullscreen mode

Piece by piece:

  • git: The tool.
  • rebase: Rebase replays your commits on top of another commit, one by one.
  • --abort: Stop the rebase and return the branch to exactly how it was before you started. Use --continue instead once you have fixed and staged a stopped step.

Say it: "Git, cancel this rebase and put my branch back how it was."

Why "git switch v1.2.0" refuses

git switch is the newer, safer command, and it will not detach HEAD unless you ask:

fatal: a branch is expected, got tag 'v1.2.0'
hint: If you want to detach HEAD at the commit, try again with the --detach option.
Enter fullscreen mode Exit fullscreen mode

To look at an old release on purpose, say so:

git switch --detach v1.2.0
HEAD is now at 8eb9869 feat: add cart taxes
Enter fullscreen mode Exit fullscreen mode

Piece by piece:

  • git: The tool.
  • switch: Move somewhere.
  • --detach: Yes, I know this is not a branch. Detach HEAD and put me on this commit.
  • v1.2.0: Where to go: the commit the v1.2.0 tag points at. A hash works too.

Say it: "Git, I know it is not a branch: take me to the v1.2.0 release so I can look around."

That is the habit worth building: look at old code with --detach, and if you start changing things, run git switch -c before your first commit.

Every message on this page was copied from Git 2.43.0 in a test repository on 1 December 2026.


This guide first appeared on jeysingh.com. It comes from my Git / GitHub Study Guide. There's a free sample at jeysingh.com/git.

Top comments (0)