ChatGPT解决这个技术问题 Extra ChatGPT

Git checkout: updating paths is incompatible with switching branches

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?

what has changed since the first instance of that command (triggering the error message) ?
git init git fetch git fetch git://blabla.com/dir1/Project.git

B
Bruno Bronosky

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

This solved the problem for me, not the above arbitrary answer.
That should be "git fetch REPOSITORY_NAME" to get all of the branches on that repository.
not necessarily. git fetch will get all of the branches from all remote repos.
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.
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.
P
Peter Mortensen

Alternate syntax,

git fetch origin remote_branch_name:local_branch_name

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.
This is not just an alternate syntax, but can work when git checkout -b branch_name origin/branch_name does not work
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!
Worked for me after running also the validated answer.
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.
C
Community

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


That's indeed an odd configuration. It tells git to only fetch the master branch from the remote.
I would vote it up 10 times if I could! - this seems to be some thing that new git does when cloning
I had the same problem, it was driving me crazy. Anyone not seeing remote branches, please check this!!
Thank god for your answer :)
Mind = blown! Thank you!
V
VirtualStaticVoid

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

O
Olivier Refalo

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


arf, just realize VirtualStaticVoid had the same solution!
+1 for git branch -v -a: I had an incorrectly configured remote that said fetch = +refs/heads/*:refs/remotes/master/* even though the remote was called upstream.
M
Matt

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.


P
Peter Mortensen

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.


E
Edward J Beckett

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

T
Thomas

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

Use git branch -a to list remote branches


C
Community

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 [] [], with [] 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.


The problem is solved. git checkout -b local-name remote/remote-branch does actually work
Interesting, what has changed since the first instance of that command (triggering the error message) ?
e
eMPee584

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.