I was on branch1
when I checkout branch2
like this (both branches are existing).
git checkout origin/branch2
then I got a detached head error:
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.
But then I just checkout branch2
(without origin
) then it works ok:
git checkout branch2
So what's the difference between git checkout
with and without origin/
; and why there was the detached HEAD
error when using origin/
?
It can be used to create branches, switch branches, and checkout remote branches. The git checkout command is an essential tool for standard Git operation. It is a counterpart to git merge . The git checkout and git merge commands are critical tools to enabling git workflows .
7 Answers. Show activity on this post. git checkout -b BRANCH_NAME creates a new branch and checks out the new branch while git branch BRANCH_NAME creates a new branch but leaves you on the same branch. In other words git checkout -b BRANCH_NAME does the following for you.
Here, branch_name is a local branch, whereas origin/branch_name is a remote-tracking branch; it reflects the state of the corresponding branch that lives in origin .
git fetch is the command that tells your local git to retrieve the latest meta-data info from the original (yet doesn't do any file transferring. It's more like just checking to see if there are any changes available). git pull on the other hand does that AND brings (copy) those changes from the remote repository.
The "detached HEAD" message is a warning, not an error.
The reason for it is simple enough. Git has two states you can be in with respect to branches:
When you are on a branch and make new commits, the branch automatically advances to include the new commits.
When you are not on a branch and make new commits, the non-branch also advances, but—here's the reason for the warning—if you then switch to some other branch (so that you are on it), git forgets where you were.1 When you are on a branch, the branch name remembers the new commits; when you are not, there is nothing to remember them.
The branch origin/branch2
is a remote-tracking branch: that is, a branch that remembers "where branch2
was on origin
the last time we (our git and origin
's git) had a conversation about branches". Because the point of this is to track where they were, git won't let you get "on" that branch and make new commits (which would then remember where you are instead of where they were).
Because you can't be on it, checking it out gives you that "detached HEAD" state instead.
The branch branch2
is a normal, ordinary, local branch. It is yours to do with as you wish. You can get on it and make new commits.
(Your local branch can also remember the remote-tracking branch, as its so-called upstream. Git's confusing terminology for this is that your local branch is then "tracking" the remote-tracking branch. The word "tracking" appears too many times here, as does the word "branch", all with different meanings.)
1Actually it saves it for a while, in the reflog for HEAD
, but this is only good for 30 days by default.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With