470

My problem is related to Fatal Git error when switching branch.

I try to fetch a remote branch with the command

git checkout -b local-name origin/remote-name

but I get this error message:

fatal: git checkout: updating paths is incompatible with switching branches.
Did you intend to checkout 'origin/remote-name' which can not be resolved as commit?

If I manually create a branch and then pull the remote branch, it works, just as making a new clone and checking the branch out.

Why does it not work on the repository I work with?

Community
  • 1
  • 1
Ikke
  • 90,705
  • 23
  • 91
  • 118
  • 1
    what has changed since the first instance of that command (triggering the error message) ? – VonC Jun 03 '09 at 19:43
  • git init git fetch git fetch git://blabla.com/dir1/Project.git –  Nov 30 '12 at 18:20

11 Answers11

743

I believe this occurs when you are trying to checkout a remote branch that your local git repo is not aware of yet. Try:

git remote show origin

If the remote branch you want to checkout is under "New remote branches" and not "Tracked remote branches" then you need to fetch them first:

git remote update
git fetch

Now it should work:

git checkout -b local-name origin/remote-name
Bruno Bronosky
  • 54,357
  • 9
  • 132
  • 120
user167628
  • 7,644
  • 1
  • 14
  • 7
  • 7
    This solved the problem for me, not the above arbitrary answer. – Jessedc Mar 10 '10 at 21:58
  • 21
    That should be "git fetch REPOSITORY_NAME" to get all of the branches on that repository. – Mike Thomsen Mar 24 '10 at 22:36
  • 1
    not necessarily. `git fetch` will get all of the branches from all remote repos. – Michael Grinich Jun 06 '10 at 23:33
  • @Michael sometimes you need to specify the remotes name like @Mike noted. – shennyg May 30 '11 at 00:49
  • 4
    In case anyone else struggles with the utter craziness of all this: git fetch origin/branchname Is _not_ the same as git fetch. The former simply results in "new (next fetch will store in remotes/origin)" displaying in a column visible via git remote show origin. – Alexander Kellett Aug 04 '11 at 13:23
  • @Mike Thomsen: your comment has been more helpful to me than the "solution". Thanks. – Flavius Aug 13 '11 at 19:13
  • This worked for me but I just checked it out, without the "-b" for new branch. – Evgeni Petrov May 03 '12 at 12:35
  • Matt's answer using git checkout is simpler. – Nick Desjardins Jan 22 '13 at 14:19
  • 1
    If you still get the same error after all these steps like I did, try Rare Pleasure's "alternate syntax" git fetch origin remote_branch_name:local_branch_name - it worked when all else failed for me – codercake Feb 12 '13 at 17:29
  • 7
    If you are trying to do this for a remote that you explicitly added (so anything *other than origin*) you will need to do `git remote update` before your fetch will fetch that remote. Otherwise you get messages like `Did you intend to checkout 'upstream-repo/master' which can not be resolved as commit?` Please add this to the answer and save people hours worth of reading the same answers that only work for origin. – Bruno Bronosky Feb 14 '13 at 20:56
  • You should mention that the tab-key completion doesn't print out the apostrophe in the branch name, and that can be a reason it doesn't find it. – NoBugs Feb 16 '13 at 16:01
  • i just do a `git pull` to make sure my local git knows about all the remote branches being tracked, and then `checkout -b` with the --track command. – courtsimas Apr 17 '13 at 14:18
  • I got into this state from a repo clone. The git remote update worked. The git fetch did NOT work until I had checked out a remote tracking branch. After that git fetch worked. – rickfoosusa Jan 30 '15 at 20:14
  • after I `git svn clone --std-layout ...`, I have no git remotes only a block of `svn-remote` in my git config. this causes any remote op to fail. Am I missing something obvious? – Mike D May 20 '15 at 23:33
151

Alternate syntax,

git fetch origin remote_branch_name:local_branch_name
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Rare Pleasures
  • 1,601
  • 1
  • 10
  • 5
  • 13
    This worked for me. My remote branch name isn't origin. I don't know if that makes a difference as I have been drinking vodka. – Rimian Nov 17 '12 at 10:55
  • 13
    This is not just an alternate syntax, but can work when git checkout -b branch_name origin/branch_name does not work – codercake Dec 20 '12 at 19:41
  • omg! fnx! u saved me from a headache! git remote update && git fetch didnt work fo me - a didnt see others branches.. – fun_vit Feb 19 '13 at 07:34
  • 1
    This also fixed my problem when running "git checkout --track origin/remote-branch", which originally gave the same error as OP's before the fix. Thanks! – kakyo Jun 11 '13 at 18:29
  • 1
    Worked for me after running also the validated answer. – AsTeR Jun 21 '13 at 08:08
  • This helped for me when started from an incomplete clone, i.e. the one with `--depth` specification. – mlt Nov 04 '14 at 23:54
  • 2
    Yes, I suspect the `--depth` qualifier for the original clone may be at fault here. I had success with `git fetch remote_branch_name:local_branch_name` but all other advice failed. – John Kelleher Apr 16 '15 at 21:01
  • This doesn't give an error message but it also doesn't associate the local branch with the remote branch as with `git checkout -b --track /` and it doesn't set the current branch to ``. So it's not a replacement for `git checkout -b --track /` at all. It just updates the remote tracking branches and creates . – bartonstanley May 03 '17 at 16:57
  • this was a life saver, the accepted didnt work, thank you! – samthet Nov 20 '20 at 15:35
46

After having tried most of what I could read in this thread without success, I stumbled across this one: Remote branch not showing up in "git branch -r"

It turned out that my .git/config file was incorrect. After doing a simple fix all branches showed up.

Going from

[remote "origin"]
    url = http://stash.server.com/scm/EX/project.git
    fetch = +refs/heads/master:refs/remotes/origin/master

to

[remote "origin"]
    url = http://stash.server.com/scm/EX/project.git
    fetch = +refs/heads/*:refs/remotes/origin/*

Did the trick

Community
  • 1
  • 1
PålOliver
  • 2,155
  • 1
  • 20
  • 23
12

Not sure if this is helpful or exactly relevant to your question, but if you are trying to fetch and checkout only a single branch from the remote repository, then the following git commands will do the trick:

url= << URL TO REPOSITORY >>
branch= << BRANCH NAME >>

git init
git remote add origin $url
git fetch origin $branch:origin/$branch
git checkout -b $branch --track origin/$branch
VirtualStaticVoid
  • 1,561
  • 3
  • 14
  • 20
11

none of the above worked for me. My situation is slightly different, my remote branch is not at origin. but in a different repository.

git remote add remoterepo GIT_URL.git
git fetch remoterepo
git checkout -b branchname remoterepo/branchname

tip: if you don't see the remote branch in the following output git branch -v -a there is no way to check it out.

Confirmed working on 1.7.5.4

Olivier Refalo
  • 46,213
  • 20
  • 84
  • 114
7

For me what worked was:

git fetch

Which pulls all the refs down to your machine for all the branches on remote. Then I could do

git checkout <branchname>

and that worked perfectly. Similar to the top voted answer, but a little more simple.

Matt
  • 371
  • 4
  • 13
4

I suspect there is no remote branch named remote-name, but that you've inadvertently created a local branch named origin/remote-name.

Is it possible you at some point typed:

git branch origin/remote-name

Thus creating a local branch named origin/remote-name? Type this command:

git checkout origin/remote-name

You'll either see:

Switched to branch "origin/remote-name"

which means it's really a mis-named local branch, or

Note: moving to "origin/rework-isscoring" which isn't a local branch
If you want to create a new branch from this checkout, you may do so
(now or later) by using -b with the checkout command again. Example:
  git checkout -b 

which means it really is a remote branch.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Don Branson
  • 13,237
  • 10
  • 54
  • 98
3

It's not very intuitive but this works well for me ...

  mkdir remote.git & cd remote.git & git init
  git remote add origin $REPO
  git fetch origin $BRANCH:refs/remotes/origin/$BRANCH        

THEN run the git branch --track command ...

  git branch --track $BRANCH origin/$BRANCH
Edward J Beckett
  • 4,701
  • 1
  • 38
  • 39
2

For me I had a typo and my remote branch didn't exist

Use git branch -a to list remote branches

Thomas
  • 6,610
  • 7
  • 40
  • 79
1

After fetching a zillion times still added remotes didn't show up, although the blobs were in the pool. Turns out the --tags option shouldn't be given to git remote add for whatever reason. You can manually remove it from the .git/config to make git fetch create the refs.

eMPee584
  • 1,726
  • 14
  • 19
1

Could your issue be linked to this other SO question "checkout problem"?

i.e.: a problem related to:

  • an old version of Git
  • a curious checkout syntax, which should be: git checkout -b [<new_branch>] [<start_point>], with [<start_point>] referring to the name of a commit at which to start the new branch, and 'origin/remote-name' is not that.
    (whereas git branch does support a start_point being the name of a remote branch)

Note: what the checkout.sh script says is:

  if test '' != "$newbranch$force$merge"
  then
    die "git checkout: updating paths is incompatible with switching branches/forcing$hint"
  fi

It is like the syntax git checkout -b [] [remote_branch_name] was both renaming the branch and resetting the new starting point of the new branch, which is deemed incompatible.

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • The problem is solved. git checkout -b local-name remote/remote-branch does actually work – Ikke Jun 03 '09 at 19:38
  • 1
    Interesting, what has changed since the first instance of that command (triggering the error message) ? – VonC Jun 03 '09 at 19:43