Use this tag for questions about a Git repository in "detached HEAD" mode. A repository is in this mode if `git status` says "HEAD detached at ..." or "HEAD detached from ..."
In a git repository, a detached HEAD is a mode where HEAD
contains a raw commit hash ID. This mode or state is perhaps best defined by contrasting it to the more typical attached HEAD state. When a repository's HEAD is attached, the special file .git/HEAD
contains the name of the current branch. This results from running git checkout branch-name
, which puts you on the branch named branch-name
. That is, your HEAD is normally attached to some branch, so that Git knows which branch you're on. But Git's HEAD is easily detached from this branch, so that it can move to another branch, or even directly to a commit.
When in this detached HEAD mode, Git will tell you that you are not on any branch. For instance, running git branch
may print:
* (HEAD detached at 3e5524907)
master
and git status
will say HEAD detached ...
, rather than on branch ...
(see git-status).
A detached HEAD is not an error state, but it is also not a typical mode in which one does work in the repository. You will most commonly see it in one of these situations:
- during a paused interactive rebase (see git-rebase), when you choose to edit some commit(s);
- during any rebase that pauses due to conflicts;
- during normal operation of most submodules (see git-submodules).
You will also end up in "detached HEAD" state by checking out any commit using any name that is not itself a branch name. For instance, git checkout origin/master
will often check out the same commit as git checkout master
, but since origin/master
is a remote-tracking name rather than a branch name, you will be in this "detached HEAD" state.
The simplest way to recover, if you are in this state unexpectedly—e.g., do not have a rebase to continue (if you are uncertain, use git status
to find out)—is just to check out a branch by name, e.g., git checkout master
. If you have made new commits in this state, however, consider creating a branch name to record them.