2

I've found an interesting scenario. This question is related to this one.

I have git (version 1.8.3.1) and I created a branch locally, then I pushed it to my remote repository. I checked (git branch -a) that it exists on the remote repository.

Now, I have the repository setup on another machine. When I run the following commands (using the version 1.8.3.1):

git fetch
git checkout <my branch>

I checkout to my branch correctly.

However, I also have the git version git version 2.6.3. And when I run the above commands, I get the error:

error: pathspec '<branch name>' did not match any file(s) known to git.

I'd expect this error in case of an older git version (see the linked question), but why does it fail for the version 2.6.3?

If I checkout to my branch using the older version of git, I can also checkout it using the git version 2.6.3.

Does it mean that git developers decided to remove this nice functionality introduced in v1.7.0-rc0 and I have to run git checkout -b <branch> --track <remote>/<branch> or am I making a mistake somewhere?

user2738748
  • 1,106
  • 2
  • 19
  • 36
  • Try `git fetch origin :refs/remotes/origin/ && git checkout `. Your `git fetch` doesn't get anything. Besides, you need to make sure that `origin` exists. If it does not, run `git remote add origin ` first. `origin` can be another name and replace `orign` in `refs/remotes/origin/` with the other name if it has already been there. – ElpieKay Oct 11 '17 at 13:28
  • Add the output of `git branch -r` to your question. I think I know the answer, but will need to refer to it. – torek Oct 11 '17 at 17:03
  • You can use `git branch -a` to check if the branch name is exist on remote. If exists, then **copy** (not input) the branch name after `git checkout`. Such as if the branch name is `–a`, there usually cause typos since `–a` is different from `-a`. So you'd better copy the branch name. – Marina Liu Oct 12 '17 at 01:44
  • Has the problem been solved? – Marina Liu Oct 24 '17 at 08:35

1 Answers1

2

If origin/mybranch does exist, a simple

git checkout mybranch 

should be enough.

So double check what git branch -r shows you after a git fetch.

If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to

$ git checkout -b <branch> --track <remote>/<branch>
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250