Me and my friend has a repo which he created. He then created a branch called "lexer" for us to work on.
The problem is that while he can switch forth and back between master and lexer it does not work at all for me.
Eventually I just started over (rm -rf repo
and then cloned the repo) but it's still impossible to checkout the lexer branch?
On a freshly cloned repo:
git branch
gives:
$ git branch * master
git checkout lexer
gives:
$ git checkout lexer $ git status On branch master Your branch is up-to-date with 'origin/master'.
I CAN checkout origin/lexer but I end up in a detached HEAD state?
$ git checkout origin/lexer master Note: checking out 'origin/lexer'. 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.
It is possible for me to push to the lexer branch by doing
git push origin HEAD:lexer
but well, I really would like to sort this mess out. It's so weird that it works for him but not for me? He says that he hasn't got any local changes from the git repo either...
Anyone have any clue?
If you want to keep changes made with a detached HEAD, just create a new branch and switch to it. You can create it right after arriving at a detached HEAD or after creating one or more commits. The result is the same. The only restriction is that you should do it before returning to your normal branch.
If you check out to the origin (main) branch, which is read-only, you will be in the detached HEAD state. Some other scenarios can cause a detached HEAD as well. For example, checking out to a specific tag name or adding ^0 on any given branch causes the detached HEAD state.
Force a Checkout You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.
I'm going to venture a guess that you have a directory named lexer
at the top level. Since git checkout
is used both to switch branches and to reset files in the tree, it's probably detecting that you don't have a branch called lexer
but you do have a path and selects the second mode. It works for your friend because he already has a lexer
branch.
Easiest workaround is probably to create the branch using git branch
instead.
git branch --track lexer origin/lexer
should do that for you. You can then use git checkout
to switch to it.
Another option might be to use the --
flag to git checkout. I haven't tried it but I think it should work:
git checkout lexer --
When you add --
, the word before it is always considered a branch/commit/tree and the word after a path.
Your probably want this:
git checkout -t origin/lexer
From git manual:
As a convenience, --track without -b implies branch creation...
and
-t, --track... When creating a new branch, set up "upstream" configuration... If no -b option is given, the name of the new branch will be derived from the remote-tracking branch
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