amigo21
amigo21

Reputation: 391

git fetch works but checkout branch doesn't work

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?

Upvotes: 2

Views: 3026

Answers (5)

th3coop
th3coop

Reputation: 438

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

Upvotes: 5

ivan kuklin
ivan kuklin

Reputation: 140

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.

Upvotes: -1

Mark Adelsberger
Mark Adelsberger

Reputation: 45819

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

Upvotes: 2

eftshift0
eftshift0

Reputation: 30297

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.

Upvotes: -1

Ry-
Ry-

Reputation: 225164

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

Upvotes: 10

Related Questions