Sub topic 12
Detached HEAD and how to leave it
This chapter assumes you can commit, read git log, and create and switch branches with git switch. It covers what a detached HEAD is, how you end up in one, how to keep work you do there, and how to leave it safely.
What "detached" means
Normally HEAD is a symbolic reference. It holds the name of a branch, and that branch holds a commit ID. When you commit, Git creates the commit and moves the branch forward. HEAD follows because it points at the branch.
In a detached HEAD state, HEAD holds a commit ID directly. No branch sits between HEAD and the commit. You can still commit, but no branch name moves with you.
cat .git/HEADOn a branch, this prints something like ref: refs/heads/main. When detached, it prints a full 40-character commit ID.
How you get into a detached HEAD
Any command that checks out something other than a local branch detaches HEAD. The usual causes are:
- Switching to a commit ID:
git switch --detach a1b2c3d. - Switching to a tag:
git switch --detach v1.2.0. Tags are fixed to one commit and do not move, so Git cannot treat them like branches. - Switching to a remote-tracking branch such as
origin/main. This is a read-only pointer that Git updates only when you fetch, so you cannot commit onto it. - Using
git checkout <commit>, the older command. It detaches without asking for--detach. - Stopping in the middle of a rebase, bisect, or cherry-pick sequence. Git detaches HEAD temporarily while it replays commits.
git switch is stricter than git checkout. If you give it a commit or tag without --detach, it refuses and explains why. That is deliberate, because detaching by accident is a common source of lost work.
git switch v1.2.0
# fatal: a branch is expected, got tag 'v1.2.0'
git switch --detach v1.2.0When you do detach, Git prints a long notice. It tells you that you are in a detached HEAD state and that commits made here belong to no branch.
Detached HEAD is useful
Detaching is not an error. These are legitimate uses:
- Inspecting how the code looked at a past commit or release.
- Building or testing an old version without touching any branch.
- Running
git bisect, which moves through history in this state. - Trying a quick experiment you expect to throw away.
Reading and running code in this state is safe. The risk starts only when you commit.
The risk: commits that no branch holds
Suppose you detach at an old commit and make two commits.
git switch --detach a1b2c3d
echo "experiment" > notes.txt
git add notes.txt
git commit -m "Try an idea"
echo "more" >> notes.txt
git commit -am "Extend the idea"HEAD moves forward with each commit, but no branch name records the new tip. Now switch away:
git switch mainGit warns you that you are leaving two commits behind. Nothing points to them any more. They still exist in the object database, and the reflog still records them, but a normal git log will not show them. Eventually garbage collection will delete them. By default, unreachable commits recorded in the reflog are kept for 30 days, and others for 90 days after being unreachable from the reflog expiry settings. Do not rely on that window. Save the work as soon as you decide it matters.
Keeping work: attach a branch
The simplest fix is to give the commits a branch name while you are still on them. If you are still detached:
git switch -c rescue-experimentThis creates a branch at the current commit and attaches HEAD to it. Your commits are now reachable from rescue-experiment. Nothing else changes in your working tree. You can also use git branch rescue-experiment to create the branch without switching to it, then switch later.
You can then merge or rebase the branch like any other.
Recovering after you already switched away
If you left the detached state before saving, find the lost commit in the reflog. The reflog is a local log of where HEAD has been.
git reflogA typical output looks like this:
4f9e1aa HEAD@{0}: checkout: moving from 7c3d2be to main
7c3d2be HEAD@{1}: commit: Extend the idea
9b8a6c1 HEAD@{2}: commit: Try an idea
a1b2c3d HEAD@{3}: checkout: moving from main to a1b2c3dThe entry 7c3d2be was the last commit you made. Create a branch there:
git branch rescue-experiment 7c3d2beBoth commits come back, because the second commit's history includes the first. Chapter 17 covers the reflog in more detail.
Leaving a detached HEAD
Pick the path that matches what you want.
| Situation | Command | Result |
|---|---|---|
| You made no commits, or you want to discard them | `git switch main` | HEAD attaches to `main`. Any detached commits become unreachable. |
| You want to keep the commits on a new branch | `git switch -c new-branch` | New branch at the current commit, HEAD attached to it. |
| You want to keep the commits on an existing branch | Make a branch first, then merge it or cherry-pick | The branch holds the commits until you integrate them. |
| You are returning to your previous location | `git switch -` | Switches to the last branch or position you were on. |
The git switch - shortcut is handy after a quick look at an old commit. If your previous position was a branch, you return to it.
Moving commits to an existing branch
Say you committed on a detached HEAD and now want those commits on main. First save them, then bring them in:
git switch -c temp-work
git switch main
git merge temp-work
git branch -d temp-workFor a single commit, you can also stay detached, note its ID, switch to main, and run git cherry-pick <commit-sha>. This works as long as you noted the ID or can find it in the reflog.
Edge cases
- Uncommitted changes. Switching to a commit with a dirty working tree works only if the changes do not conflict with the target. Otherwise Git refuses. Commit, or stash with
git stash, first. - A branch already at that commit. Detaching at a commit that a branch also points to is not a problem. Detached HEAD is about how HEAD is stored, not about the commit.
- Pushing.
git pushfrom a detached HEAD has no current branch to push, so a plaingit pushfails. You can push a specific commit to a named remote branch withgit push origin HEAD:refs/heads/new-branch, but it is clearer to create the local branch first. - Status messages.
git statusshowsHEAD detached at <id>when HEAD still sits where you detached. It showsHEAD detached from <id>once you have moved away with commits. - Prompts. Many shell prompts show the short commit ID or
(HEAD detached at ...)instead of a branch name. Watch for this before you start working.
Common errors
- Committing on a detached HEAD without noticing. Check
git statusorgit branch --show-current. The latter prints nothing when you are detached. - Assuming a tag is a branch. You cannot commit onto a tag. Make a branch from it if you want to build on it:
git switch -c hotfix-1.2 v1.2.0. - Panicking after the warning. The warning on leaving is informational. Copy the commit ID it prints, then create a branch at that ID.
git branch --show-current
git statusExercise
You detached HEAD at tag v2.0, made three commits, and then ran git switch main. Git warned that you left commits behind and printed the ID of the last one, 5d6e7f8. How do you get the work back on a branch, and why does it work?
Show answer
Run git branch recovered-work 5d6e7f8, or git switch -c recovered-work 5d6e7f8 to also move onto it. The commits still exist in the object database, and each commit points to its parent. A branch at the last commit makes all three reachable. If you lost the ID, find it with git reflog.
Exercise
Why does git switch origin/main leave you with a detached HEAD, and how do you work on that code with a normal branch instead?
Show answer
origin/main is a remote-tracking branch, which Git updates only on fetch, so you cannot commit onto it. Git detaches HEAD instead. To work on it, create a local branch from it with git switch main if it already exists, or git switch -c my-branch origin/main to start a new local branch at that commit.