0

I'm trying to checkout a branch that I've just fetched from my upstream remote repo but it doesn't seem to work.

$ git fetch upstream
Fetching upstream
From github.com:group/repo
* [new branch]      feature-branch -> upstream/feature-branch

$ git checkout feature-branch
error: pathspec 'feature-branch' did not match any file(s) known to git.

Am I doing something wrong?

amigo21
  • 291
  • 3
  • 15

5 Answers5

10

The branch is likely present in more than one remote. (You can confirm this with git branch --list --remotes '*/feature-branch'.) git checkout only creates branches like that if they’re unambiguous. From git-checkout(1):

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>

So you’ll need to do that instead:

git checkout -b feature-branch --track upstream/feature-branch
Community
  • 1
  • 1
Ry-
  • 199,309
  • 51
  • 404
  • 420
2

You're wanting git to understand the "shortcut" checkout notation, but it seems to find it inapplicable. Perhaps do multiple remotes have branches named feature_branch?

Well, anyway, git checkout -b feature-branch -track upstream/feature-branch ought to work

Mark Adelsberger
  • 32,904
  • 2
  • 24
  • 41
1

This post solved it for me. I had forgotten that I had done a shallow clone of the repo. How to convert a Git shallow clone to a full clone?

The below command (git version 1.8.3) will convert the shallow clone to regular one

git fetch --unshallow

Then, to get access to all the branches on origin (thanks @Peter in the comments)

git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch origin
th3coop
  • 316
  • 2
  • 10
  • 1
    Thanks. I was going crazy about what could it be and seeing your answer made me remember I also had a shallow clone only :) – dragosb Sep 25 '20 at 19:08
-1

There are some automations that can happen when you ask to checkout a local branch that doesn't exist (create it from some remote branch, for example), but this won't fail for you: git checkout upstream/feature-branch. The only thing is that no local branch is being created.

eftshift0
  • 16,836
  • 2
  • 23
  • 36
-1

When you run git checkout feature-branch git try to remove all unsaved changes in file named feature-branch. For checkout your branch use -b option like this git checkout -b feature-branch.

ivan kuklin
  • 140
  • 2
  • 7
  • This checkouts out a NEW branch of that name base on whatever the current checked out branch is. This will not checkout the remote branch. – th3coop Mar 05 '19 at 00:33