1

I have a curious issue with a branch in github that is on my remote, e.g.

$ git branch -r
...
adler/issue-761__NLM
...

But when I try to checkout the branch, I get an error

$ git checkout issue-761__NLM
error: pathspec 'issue-761__NLM' did not match any file(s) known to git.

I have tried git fetch etc, but it does not seem to resolve the issue. Curiously, I can explicitly checkout the branch from the remote, but this puts me in a detached head state

$ git checkout adler/issue-761__NLM
...    
HEAD is now at 94df12a... CONTRIB: Updates to NLM functional

How do I go about to resolve this issue so that I can check out the branch locally and work on it?

Edit: I've been told that is is a duplicate of How do I check out a remote Git branch?. That is not the case, specifically I can in fact perform git checkout adler/issue-761__NLM and further, git fetch does not solve my issue.

Jonas Adler
  • 10,365
  • 5
  • 46
  • 73

2 Answers2

3

git checkout issue-761__NLM doesn't work because (most probably) there is no local branch named issue-761__NLM.

You can either create the local branch and after that check it out:

$ git branch issue-761__NLM adler/issue-761__NLM
$ git checkout issue-761__NLM

Or, you can do both operations in a single command by running git checkout with the correct arguments:

$ git checkout -b issue-761__NLM

If there is no ambiguity where the remote branch(es) named issue-761__NLM points to, behind the scene, this command runs the two commands listed above.


Why it used to work?

The documentation for git checkout <branch> says:

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>

Please note the "exactly one remote" magic formula.
I guess you just added a secondary remote that also contains the issue-761__NLM branch.

Community
  • 1
  • 1
axiac
  • 68,258
  • 9
  • 99
  • 134
  • Thank you very much, this solves the issue. Do you have any idea why I can fail to get a local branch? In all of my git history I've always been able to simply do `git checkout mybranch` and it gave me the correct result. – Jonas Adler Jul 14 '17 at 13:06
  • The second option assumes that the correct start point is checked out, and still doesn't set up tracking. The correct shorthand (when just `checkout branch_name` doesn't work) would be `checkout --track remote/branch_name` – Mark Adelsberger Jul 14 '17 at 13:07
  • The [documentation](https://git-scm.com/docs/git-checkout#git-checkout-emgitcheckoutemltbranchgt) for `git checkout ` says *"If `` is not found but there does exist a tracking branch in exactly one remote (call it ``) with a matching name, treat as equivalent to `$ git checkout -b --track /`"* -- I guess you just added a secondary remote that also contains the `issue-761__NLM` branch. – axiac Jul 14 '17 at 13:10
  • TriskalJM, had "you cannot accept within", done now. axiac, that does indeed seem to be the correct explanation. – Jonas Adler Jul 14 '17 at 13:11
2

For some reason the "shorthand" that automatically creates local branches from remote branches isn't applicable in this case. I think the most likely reason would be if you have a second remote that also has a branch named issue-761__NLM

You can specify that adler/issue-761__NLM is indeed the correct remote branch to track by saying

git checkout --track adler/issue-761__NLM

Adding --track to the checkout will tell git to create the local branch and set up tracking, rather than put you in detached HEAD state.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52