0

I have a git repo with two remote.

    origin/master        cm/master
         |                  |
         |                  |
       sfcc--------------cm/sfcc-18
         |
         |
       feature

The above is the basic structure of my repo. Branch sfcc is pulled from another branch sfcc from cm.

Both the branches sfcc and feature are also pushed to the remote origin.

Branch sfcc is not yet merged to the origin/master.

Now I am on the local branch feature. I have not yet done any changes other than creating these branches. I try to switch to origin/sfcc which puts me in a detached state.

HEAD is now at 9d4f1707 Merge branch 'sfcc-18' of https://github.com/bla-bla-cm into sfcc.

Again when I try to checkout origin/migration I get the same message.

On git status i get working tree clean and nothing to commit.

Why do I get into this situation even when nothing was changed?

In case something has changed on cm/sfcc-18, I do not want it to be tracked with respect to the cm repo changes. I have set the upstream tracking for origin/sfcc with origin/master.

I cannot checkout origin/master as well. I get the following message

Previous HEAD position was 9d4f1707 Merge branch 'sfcc-18' of https://github.com/bla-bla-cm into sfcc

HEAD is now at e71242f0 .gitignore created online with Bitbucket

Community
  • 1
  • 1
Vini
  • 1,978
  • 8
  • 40
  • 82
  • *"Again when I try to checkout origin/migration I get the same message."* -- **how** do you checkout `origin/migration`? – axiac Mar 27 '18 at 09:35
  • `git checkout origin/master` – Vini Mar 27 '18 at 09:47
  • The [answer](https://stackoverflow.com/a/49509397/4265352) provided below by @cody-hamilton explains why this command produces the outcome you get (and you don't want) and how to use [`git checkout`](https://git-scm.com/docs/git-checkout) correctly. The documentation [available online](https://git-scm.com/docs/git-checkout) can also be invoked using the command line as `git help checkout` (replace `checkout` with other Git commands you need to learn about.) – axiac Mar 27 '18 at 09:52
  • Yes I have understood that. :-) – Vini Mar 27 '18 at 09:53

1 Answers1

3

The most likely reason you are seeing your checkouts result in detached head is that you are referencing the branch you want to check out as origin/<branch-name> instead of simply <branch-name>.

While technically both will result in HEAD being at the same commit, the way Git behaves in the two cases is different.

git checkout origin/<branch> checks out a specific commit as a detached head, even if you have a local branch with the same name, as if you had used --detach

git checkout <branch> will check out a local branch, if it exists. If it doesn't, and a single remote tracked branch does exist with that name, it will create a local branch with that name and set it to track the remote branch. See git checkout for more information.

In your case, you should be able to just type git checkout sfcc-18, and a new local branch will be checked out that will track cm/sfcc-18.

Note however if you wanted to checkout origin's master as a new local branch, you would need git checkout -b master --track origin/master

Cody Hamilton
  • 331
  • 1
  • 4
  • Since I am fairly new using git, i couldn't distinguish the differnce between `checkout origin/sfcc` and `checkout sfcc` . As you mentioned, when I checkout without the origin, I am all good to work. Is there any situation when I will have to do `checkout origin/sfcc` . – Vini Mar 27 '18 at 09:46
  • While you don’t have to use the form `checkout origin/branch` it can be useful in some circumstances. Often I just want to review some code or make a quick change, push it to the remote and be done. Having a detached head is great in these situations because you don’t have to delete your local branch afterward. – Cody Hamilton Mar 28 '18 at 01:13
  • Thanks a lot for the explanation. Now I have understood what I have been doing wrong all this time. – Vini Mar 28 '18 at 07:49