6

I'm trying to checkout a remote branch which doesn't exist locally.

git checkout -b branch_name origin/branch_name

gives:

fatal: Cannot update paths and switch to branch 'branch_name' at the same time.
Did you intend to checkout 'origin/branch_name' which can not be resolved as commit?

git branch -a doesn't show the branch I'm trying to checkout.

How can I checkout the remote branch locally?

Zoe
  • 27,060
  • 21
  • 118
  • 148
MeesterPatat
  • 2,671
  • 9
  • 31
  • 54

2 Answers2

12

Try this

git remote update
git fetch
git checkout -b branch_name origin/branch_name

Your local repo is not aware of the remote branch.

abhilashv
  • 1,418
  • 1
  • 13
  • 18
  • I tried but it didn't work. Checkout still gives the same error message(fatal: Cannot update paths and switch to branch 'branch_name' at the same time). – MeesterPatat Jul 08 '15 at 08:26
  • The first two commands work, but `git checkout -b branch_name origin/branch_name` just gives the same error "fatal: Cannot update paths and switch to branch 'branch_name' at the same time." on git 2.12 – SteveExdia Oct 14 '21 at 15:44
2

If git branch -a doesn't show the branch you want, it doesn't exist on the remote either - the 'origin/branch_name' which can not be resolved message confirms that.

First, run git fetch origin to sync your local snapshot of the remote and see if the remote branch appears in git branch -a. In that case your current command should work, or there are many other versions in Checkout remote Git branch.

If the remote branch doesn't appear, you'll need to create it with

git checkout -b branch_name
git push -u origin branch_name

You might also want to check git remote -v to make sure your remote exists and is called origin.

Kristján
  • 18,165
  • 5
  • 50
  • 62
  • The remote branch definitely does exist. git branch -a shows: develop * CurrentBranch remotes/origin/HEAD -> origin/develop remotes/origin/develop I did try git fetch origin but it didn't make a difference. git remote -v shows: origin https://github.com/reponame/reponame.git (fetch) origin https://github.com/reponame/reponame.git (push) – MeesterPatat Jul 08 '15 at 08:19
  • It looks like the only branch `origin` is aware of is `develop`. Which branch are you trying to check out that you see `origin` having? – Kristján Jul 08 '15 at 08:23
  • The branch i'm trying to checkout is called develop2. I can see the branch on github, but it doesn't show up in my command outputs. – MeesterPatat Jul 08 '15 at 08:27
  • Does `develop2` exist on GitHub in the same `user/repo` your `origin` is pointing to? If the repository was forked, it might be in a different copy that you need to add as a second remote. – Kristján Jul 08 '15 at 08:29
  • It does exist in the same user/repo. I tried git branch -a on my laptop and develop2 does show up. So it's only a problem on my current pc. – MeesterPatat Jul 08 '15 at 08:33
  • 2
    It sounds like something funny might have happened to your first computer's copy then, which'll be hard to help debug much further. I'd try `git fetch --all` and compare `.git/config` between your machines, then consider a fresh `git clone`. – Kristján Jul 08 '15 at 08:43
  • I did a fresh git clone, and now I can checkout. – MeesterPatat Jul 09 '15 at 10:35