I ended up with a detached head today, the same problem as described in: git push says everything up-to-date even though I have local changes
As far as I know I didn
Detached HEAD
means that what's currently checked out is not a local branch.
Some scenarios that will result in a Detached HEAD
state:
If you checkout a remote branch, say origin/master
. This is a read-only branch. Thus, when creating a commit from origin/master
it will be free-floating, i.e. not connected to any branch.
If you checkout a specific tag or commit. When doing a new commit from here, it will again be free-floating, i.e. not connected to any branch. Note that when a branch is checked out, new commits always gets automatically placed at the tip.
When you want to go back and checkout a specific commit or tag to start working from there, you could create a new branch originating from that commit and switch to it by git checkout -b new_branch_name
. This will prevent the Detached HEAD
state as you now have a branch checked out and not a commit.
It can happen if you have a tag named same as a branch.
Example: if "release/0.1" is tag name, then
git checkout release/0.1
produces detached HEAD at "release/0.1". If you expect release/0.1 to be a branch name, then you get confused.
The other way to get in a git detached head state is to try to commit to a remote branch. Something like:
git fetch
git checkout origin/foo
vi bar
git commit -a -m 'changed bar'
Note that if you do this, any further attempt to checkout origin/foo will drop you back into a detached head state!
The solution is to create your own local foo branch that tracks origin/foo, then optionally push.
This probably has nothing to do with your original problem, but this page is high on the google hits for "git detached head" and this scenario is severely under-documented.
try
git reflog
this gives you a history of how your HEAD and branch pointers where moved in the past.
e.g. :
88ea06b HEAD@{0}: checkout: moving from DEVELOPMENT to remotes/origin/SomeNiceFeature e47bf80 HEAD@{1}: pull origin DEVELOPMENT: Fast-forward
the top of this list is one reasone one might encounter a DETACHED HEAD state ... checking out a remote tracking branch.
I reproduced this just now by accident:
lists the remote branches
git branch -r
origin/Feature/f1234
origin/master
I want to checkout one locally, so I cut paste:
git checkout origin/Feature/f1234
Presto! Detached HEAD state
You are in 'detached HEAD' state. [...])
Solution #1:
Do not include origin/
at the front of my branch spec when checking it out:
git checkout Feature/f1234
Solution #2:
Add -b
parameter which creates a local branch from the remote
git checkout -b origin/Feature/f1234
or
git checkout -b Feature/f1234
it will fall back to origin automatically
A simple accidental way is to do a git checkout head
as a typo of HEAD
.
Try this:
git init
touch Readme.md
git add Readme.md
git commit
git checkout head
which gives
Note: checking out 'head'.
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 performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at 9354043... Readme