Git checkout:更新路径与切换分支不兼容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/945654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:32:57  来源:igfitidea点击:

Git checkout: updating paths is incompatible with switching branches

gitgit-checkoutremote-branch

提问by Ikke

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

我的问题与切换分支时的致命 Git 错误有关。

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?

致命:git checkout:更新路径与切换分支不兼容。
您是否打算签出无法解析为提交的“来源/远程名称”?

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?

为什么它不适用于我使用的存储库?

回答by user167628

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 repo 尚不知道的远程分支时,会发生这种情况。尝试:

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

回答by Rare Pleasures

Alternate syntax,

替代语法,

git fetch origin remote_branch_name:local_branch_name

回答by P?lOliver

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"

在尝试了我可以在这个线程中阅读的大部分内容但没有成功之后,我偶然发现了这个: 远程分支没有出现在“git branch -r”中

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

结果是我的 .git/config 文件不正确。在做了一个简单的修复之后,所有的分支都出现了。

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

成功了

回答by VirtualStaticVoid

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

不确定这是否对您的问题有帮助或完全相关,但如果您试图从远程存储库中仅获取和签出一个分支,那么以下 git 命令将起作用:

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

回答by 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.

以上都不适合我。我的情况略有不同,我的远程分支不在origin。但在不同的存储库中。

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 -athere is no way to check it out.

提示:如果您在以下输出中没有看到远程分支,git branch -v -a则无法检查它。

Confirmed working on 1.7.5.4

确认在 1.7.5.4 上工作

回答by 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.

这工作得很好。类似于投票最高的答案,但更简单一点。

回答by Don Branson

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

我怀疑没有名为 remote-name 的远程分支,但您无意中创建了一个名为 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:

因此创建一个名为 origin/remote-name 的本地分支?键入此命令:

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.

这意味着它确实是一个远程分支。

回答by Eddie B

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 命令...

  git branch --track $BRANCH origin/$BRANCH

回答by Thomas

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

对我来说,我有一个错字,我的远程分支不存在

Use git branch -ato list remote branches

使用git branch -a到列表远程分支机构

回答by eMPee584

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

在获取了无数次之后,尽管 blob 在池中,但仍然没有出现添加的遥控器。事实证明,无论出于何种原因,都不应该提供--tags选项git remote add。你可以手动从 .git/config 中删除它,让 git fetch 创建 refs。