"无法同时更新路径并切换到分支"

mar*_*ful 181 git git-checkout

我有时会使用该checkout -b选项创建一个新分支,同时检查它并在一个命令中设置跟踪.

在新环境中,我收到此错误:

$ git checkout -b test --track origin/master
fatal: Cannot update paths and switch to branch 'test' at the same time.
Did you intend to checkout 'origin/master' which can not be resolved as commit?
Run Code Online (Sandbox Code Playgroud)

为什么Git不喜欢它?这曾经使用相同的回购.

Von*_*onC 191

' origin/master'无法解决为提交

奇怪:你需要检查你的遥控器:

git remote -v
Run Code Online (Sandbox Code Playgroud)

并确保origin获取:

git fetch origin
Run Code Online (Sandbox Code Playgroud)

然后:

git branch -avv
Run Code Online (Sandbox Code Playgroud)

(看看你是否有origin/master分支)

  • 我的命令中有一个拼写错误,触发了这个错误; 我没有正确拼写我的遥控器! (9认同)
  • 这是一个新分支,我的本地存储库不知道它。我不得不做一个`pull`,然后这个命令就起作用了。 (2认同)
  • @coding_idiot一个`git fetch`已经足够了. (2认同)
  • 这个答案很有用,因为它向我展示了真的有些奇怪的事情:遥控器设置正确,但是新的远程分支根本没有被提取.当我在一个干净的目录中克隆遥控器时,它可以工作.我的`.git`目录可能会以某种方式腐败吗? (2认同)

Lud*_*der 73

FWIW:如果你的分支机构中有拼写错误,你会得到同样的错误.

  • 是的,这是我的问题,我的分支名称中有一个空格 (7认同)
  • 实际上,如果有人可以花时间告诉Git开发人员这样做会很好,错误信息不是很有用;) (4认同)

Bob*_*man 53

您可以在上下文中获取此错误,例如Travis构建,默认情况下,检查代码git clone --depth=50 --branch=master.据我所知,你可以控制--depth通过.travis.yml,但不是--branch.由于这导致只有一个分支被遥控器跟踪,您需要独立更新遥控器以跟踪所需的遥控器的参考.

之前:

$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
Run Code Online (Sandbox Code Playgroud)

修复:

$ git remote set-branches --add origin branch-1
$ git remote set-branches --add origin branch-2
$ git fetch
Run Code Online (Sandbox Code Playgroud)

后:

$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/branch-1
remotes/origin/branch-2
remotes/origin/master
Run Code Online (Sandbox Code Playgroud)

  • 为我解决了它.我使用了一个浅的--depth = 1克隆,这就是我需要切换分支. (5认同)

小智 19

这个简单的事对我有用!

如果它说它不能同时做两件事,那就将它们分开.

git branch branch_name origin/branch_name 

git checkout branch_name
Run Code Online (Sandbox Code Playgroud)


ssa*_*asi 9

遇到这个问题时,您可以按照以下步骤操作:

  1. 运行以下命令以列出本地存储库已知的分支.

git远程显示来源

输出这个:

 remote origin
  Fetch URL: <your_git_path>
  Push  URL: <your_git_path>
  HEAD branch: development
  Remote branches:
    development                             tracked
    Feature2                                tracked
    master                                  tracked
    refs/remotes/origin/Feature1         stale (use 'git remote prune' to remove)
  Local branches configured for 'git pull':
    Feature2     merges with remote Feature2
    development  merges with remote development
    master       merges with remote master
  Local refs configured for 'git push':
    Feature2     pushes to Feature2     (up to date)
    development  pushes to development (up to date)
    master       pushes to master      (local out of date)
Run Code Online (Sandbox Code Playgroud)
  1. 在验证了诸如(获取URL等)之类的详细信息之后,运行此命令以获取远程但不在本地中的任何新分支(即您可能希望在本地仓库中签出).
» git remote update

Fetching origin
From gitlab.domain.local:ProjectGroupName/ProjectName
 * [new branch]      Feature3    -> Feature3
Run Code Online (Sandbox Code Playgroud)

如您所见,新分支已从远程获取.
3.最后,使用此命令检出分支

» git checkout -b Feature3 origin/Feature3

Branch Feature3 set up to track remote branch Feature3 from origin.
Switched to a new branch 'Feature3'
Run Code Online (Sandbox Code Playgroud)

没有必要明确告诉Git使用远程跟踪(使用--track)分支.

上面的命令将设置本地分支以从源跟踪远程分支.


小智 7

如果您的分支中有空白,那么您将收到此错误。