git detached
Detached HEAD state
Say you've fetched the latest changes from a remote repository with git fetch
and then run git checkout origin/main to look at them. Git warns you that you're
in a "detached HEAD" state and suggests creating a branch with
git switch -c <branch-name>.
Have you ever wondered what that really means, and how you end up in this situation? In this post, I'll explain why Git shows this warning and how a user reaches this state. To understand it, let's look at some of the internal state Git keeps to do its work.
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 dbdb125e ci: label and close issues and pull requests that went stale
git checkout origin/main. origin/main is a
remote-tracking branch, not your local main, so Git checks out its commit directly.
The HEAD file
Git works from the information stored in the .git directory. As long as that
directory is intact, you can get your work back even after deleting a file or folder from your
source code, provided the changes were committed. One of the most important files in
.git is HEAD. It tells Git what is currently checked out. Usually it
holds a symbolic reference to the current branch, and that branch's file holds the ID
of its latest commit.
% cat .git/HEAD
ref: refs/heads/main
% ls .git/refs/heads
demo main
% cat .git/refs/heads/main
dbdb125edbdf2cc83344669cd50f8c07d723668a
.git/refs/heads may not list every branch. After git gc, or in
a fresh clone, some branches are stored in .git/packed-refs instead.
Here, .git/HEAD contains ref: refs/heads/main, which points to another
file. That file, .git/refs/heads/main, contains the commit ID
dbdb125edbdf2cc83344669cd50f8c07d723668a. The .git/refs/heads
directory holds the head (the latest commit) of each local branch in the repository.
Sometimes HEAD holds a commit ID directly instead of a branch reference. This
happens when you check out a tag, a specific commit, or a remote-tracking branch such as
origin/main. It also happens for a while during operations like
git rebase and git bisect. In these cases, Git is in a detached HEAD
state.
% git checkout e1a735d3
Note: switching to 'e1a735d3'.
% cat .git/HEAD
e1a735d3f697880c28cc9ca4350e466834150d1b
HEAD now contains a commit ID instead of
ref: refs/heads/main.
We say HEAD is detached when the HEAD file contains a commit ID rather
than a symbolic reference to a branch. In this state, there is no branch for Git to move forward
when you make new commits. Git updates only HEAD itself, so your new commits aren't
attached to any branch name. Once you switch away, nothing points to them, and they become easy
to lose. This is why Git tells you to create a branch when you check out a tag, a commit ID, and
so on.
If you've already switched away, the commits aren't gone yet. You can find them with
git reflog and attach a branch to them with
git branch <name> <commit-id>, until Git eventually cleans up
unreachable commits.