git 不能同时创建本地和远程分支(跟踪)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17137441/
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-19 08:39:21  来源:igfitidea点击:

Can not create a local and remote branch (tracking) at the same time

gitversion-controlgit-branchgit-remote

提问by Jim

From Pro Git:

来自Pro Git

you can set up other tracking branches if you wish — ones that don't track branches on origin and don't track the master branch. The simple case is the example you just saw, running git checkout -b [branch] [remotename]/[branch]

$ git checkout --track origin/serverfix Branch serverfix set up to track remote branch refs/remotes/origin/serverfix. Switched to a new branch "serverfix"

$ git checkout -b sf origin/serverfix Branch sf set up to track remote branch refs/remotes/origin/serverfix. Switched to a new branch "sf"

如果您愿意,您可以设置其他跟踪分支——那些不跟踪原始分支和不跟踪主分支的分支。简单的例子就是你刚才看到的例子,运行 git checkout -b [branch] [remotename]/[branch]

$ git checkout --track origin/serverfix 分支 serverfix 设置为跟踪远程分支 refs/remotes/origin/serverfix。切换到新分支“serverfix”

$ git checkout -b sf origin/serverfix 分支 sf 设置为跟踪远程分支 refs/remotes/origin/serverfix。切换到新分支“sf”

My understanding is that this presents a way to create a local branch and an upstream branch.

我的理解是,这提供了一种创建本地分支和上游分支的方法。

But when I do:

但是当我这样做时:

git checkout -b iss53 origin/iss53I get:
fatal: Cannot update paths and switch to branch 'iss53' at the same time.

git checkout -b iss53 origin/iss53我得到:
fatal: Cannot update paths and switch to branch 'iss53' at the same time.

And when I do:
git checkout --track origin/iss53I get:

当我这样做时:
git checkout --track origin/iss53我得到:

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

致命:无法同时更新路径和切换到分支“iss53”。您是否打算签出无法解析为提交的“origin/iss53”?

Why?

为什么?

回答by VonC

Cannot update paths and switch to branch

As I mention in "Get new upstream branch with git", you can try:

正如我在“使用 git 获取新的上游分支”中提到的,您可以尝试:

# let's create a new local branch first
git checkout -b iss53
# then reset its starting point
git reset --hard origin/iss53 

Make sure that the remote tracking branchorigin/iss53does exist first (after a git fetch origin)

首先确保远程跟踪分支origin/iss53确实存在(在 a 之后git fetch origin

origin/iss53means there was a iss53on the upstreamremote repo referenced by origin.

origin/iss53装置有一个iss53上游通过引用的远程回购origin

If there was not such a branch, then you only create a local branch iss53, and push it like so:

如果没有这样的分支,那么您只需创建一个本地分支iss53,然后像这样推送它:

git push -u origin iss53 

That would establish an association between the local branch iss53and the remote tracking branch origin/iss53(tracking the newly created branch iss53on origin, created by the push).

这将建立本地分支机构之间的关联iss53和远程跟踪分支origin/iss53(跟踪新创建的分支iss53origin,通过推动创建)。

See "Why do I need to explicitly push a new branch?" for more on that initial push.

有关初始推送的更多信息,请参阅“为什么我需要明确推送新分支?”。

回答by Diego V

Seems like you hadn't fetch that commit yet. So, first do:

好像你还没有获取那个提交。所以,首先做:

git fetch origin

And then:

进而:

git checkout --track origin/iss53

回答by PESMITH_MSFT

FWIW for other people with the same error message: when this happened to me, the underlying problem was that I was trying to make a branch with spaces in the name. For the set of pre-canned GIT commands I had, branches with spaces were a problem.

FWIW 对于其他具有相同错误消息的人:当这发生在我身上时,潜在的问题是我试图创建一个名称中带有空格的分支。对于我拥有的一组预装 GIT 命令,带有空格的分支是一个问题。

(ObDisclaimer: I am very, very far from a git expert. I just know that I had a problem with an identical error message, and the solution was different than the accepted answer)

(ObDisclaimer:我离一个 git 专家非常非常远。我只知道我有一个相同的错误消息的问题,并且解决方案与接受的答案不同)