git 致命:您当前分支的上游分支与您当前分支的名称不匹配

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

fatal: The upstream branch of your current branch does not match the name of your current branch

gitpushremote-branch

提问by Aaron Digulla

After doing a checkout of the remote branch releases/rel_5.4.1using the Git GUI, I'm seeing this unexpected error message when I try to push:

releases/rel_5.4.1使用 Git GUI签出远程分支后,当我尝试执行push以下操作时,我看到了此意外错误消息:

fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use

    git push origin HEAD:releases/rel_5.4.1

To push to the branch of the same name on the remote, use

    git push origin rel_5.4.1

I don't know what Git is talking about. I probably want to push to origin releases/rel_5.4.1since that's the branch which I checked out. So neither option seems correct to me.

我不知道 Git 在说什么。我可能想推动,origin releases/rel_5.4.1因为那是我签出的分支。所以这两个选项对我来说似乎都不正确。

git statussays I'm on branch rel_5.4.1.

git status说我在 branch rel_5.4.1

Here is the branch as it appears in my .git/config:

这是出现在 my 中的分支.git/config

[branch "rel_5.4.1"]
    remote = origin
    merge = refs/heads/releases/rel_5.4.1

What is going on?

到底是怎么回事?

采纳答案by Aaron Digulla

Your local branch is called rel_5.4.1but the remote branch is releases/rel_5.4.1(as far as Git is concerned, the /has no special meaning in branch names except to make them easier to read for the human eye).

您的本地分支被调用,rel_5.4.1但远程分支被调用releases/rel_5.4.1(就 Git 而言,/分支名称中没有特殊含义,只是为了使它们更容易被人眼阅读)。

When you push, Git is wary whether you want to push your branch to releases/rel_5.4.1(the name of the remote branch) or whether you want to create a new remote branch. It does notice the similarity of names, though.

当你推送时,Git 会小心你是要推送你的分支到releases/rel_5.4.1(远程分支的名称)还是要创建一个新的远程分支。不过,它确实注意到名称的相似性。

Unless you want to create a new branch, the correct command is

除非你想创建一个新的分支,否则正确的命令是

git push origin HEAD:releases/rel_5.4.1

You could also use

你也可以使用

git push origin rel_5.4.1:releases/rel_5.4.1

To fix the warning once and for all, rename your local branch to match the remote name:

要一劳永逸地修复警告,请重命名本地分支以匹配远程名称:

git branch -m releases/rel_5.4.1

回答by Tino

For the benefit of the readers who might miss the probably most important detail, well hidden in the comments:

为了那些可能会错过可能最重要的细节的读者的利益,这些细节隐藏在评论中:

This is due to the git config push.defaultsetting.It defines what gitdoes when you enter git push(see link).

这是git config push.default设置的原因。它定义了git您输入时的操作git push请参阅链接)。

In the question, apparently the setting was set to simple(which is the default for git v2), probably with

在这个问题中,显然设置被设置为simple(这是 的默认值git v2),可能与

git config --global push.default simple

This means, that gitrefuses to push when the local and remote branch do not match exactly.

这意味着,git当本地和远程分支不完全匹配时,它拒绝推送。

As @TomSpurling notes, above setting is safer and recommended for normal use, because usually you want the same names for your local and remote branches.

However in certain situations, when your local branch is tracking some different remote branch with a different name, then you want to change that:

正如@TomSpurling 所指出的,上述设置更安全,建议正常使用,因为通常您希望本地和远程分支具有相同的名称。

但是在某些情况下,当您的本地分支使用不同的名称跟踪某个不同的远程分支时,您需要更改它:

To allow to push to the tracking branch on a per-git basis, thus make git pulland git pushsymmetric, use

为了允许在每个 git 的基础上推送到跟踪分支,因此makegit pullgit pushsymmetric,使用

git config push.default upstream

Note:To globally set this for all of your gits, use git config --global push.default upstream
However it is probably better to leave it to git config --global push.default simpleand only set this option in those workloads, where it is really required.

注意:要为您git的所有s全局设置此选项,请使用git config --global push.default upstream
但是最好将其保留git config --global push.default simple并仅在那些真正需要的工作负载中设置此选项。

回答by s.meijer

This error can be fixed for once and all, with:

这个错误可以一劳永逸地修复,使用:

git branch releases/rel_5.4.1 -u origin/releases/rel_5.4.1

It changes the upstream of the branch, to match the correct remote (again).

它更改分支的上游,以匹配正确的远程(再次)。

回答by gpap

In my case git branch --unset-upstreamsolved that issue.

在我的情况下 git branch --unset-upstream解决了这个问题。

回答by hek2mgl

Seems like having a local branch name which is different than the remote is not what Gitlikes too much. You will need to issue:

似乎拥有一个与远程分支不同的本地分支名称并不太Git喜欢。您需要发出:

git push origin HEAD:releases/rel_5.4.1

explicitely on every push

每次推送都明确