为什么“git push helloworld +master:master”而不是“git push helloworld”?

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

Why "git push helloworld +master:master" instead of just "git push helloworld"?

gitgit-push

提问by S. Michaels

I tried to push my (first ever!) git repo like this initially:

我最初尝试像这样推送我的(有史以来第一次!)git repo:

$ git push helloworld

But I got this back:

但我得到了这个:

To [email protected]:helloworld.git
 ! [rejected]        HEAD -> master (non-fast forward) error:
 failed to push some refs to '[email protected]:helloworld
git'

So I found another StackOverflow questionabout "amended commits" and tried a suggestion from there without really knowing whether it would help me:

所以我发现了另一个关于“修正提交”的StackOverflow 问题,并尝试了从那里提出的建议,但并不知道它是否对我有帮助:

KaiserSosa@SMICHAELS /c/test/helloworld (master)
$ git push helloworld +master:master

It worked!

有效!

But I don't know why it fixed my problem :(

但我不知道为什么它解决了我的问题:(

Could someone explain why this works but "git push helloworld" doesn't?

有人可以解释为什么这行得通,但“ git push helloworld”不行?

回答by VonC

It appears you have rewritten your history (SHA-1 associated with your commit) in your master branch.

看来您已经在您的主分支中重写了您的历史记录(与您的提交相关联的 SHA-1)。

That means, you can no longer push in a fast-forward mode.

这意味着,您不能再进入快进模式。

the +master forces the push to take place:
By having the optional leading +, you can tell git to update the <dst>ref even when the update is not a fast forward.

+master 强制推送发生:
通过使用可选的前导 +,<dst>即使更新不是快进,您也可以告诉 git 更新ref。

Note: this could be bad if anyone else has already cloned your repository, since they will no longer be able to just pull your master branch without having some conflict.
See also this SO answer for more.

注意:如果其他人已经克隆了您的存储库,这可能会很糟糕,因为他们将不再能够在没有冲突的情况下拉取您的主分支。
另请参阅此SO 答案以了解更多信息



Note: as mentioned by Junio C. Hamano:

注意:正如Junio C. Hamano所提到

There are two independent safety mechanisms:

  • the sending end safety can be overridden by "git push --force" and/or by using a refspec prefixed with a '+');

  • the receiving end safety can be overridden by the config variable receive.denynonfastworwardsof the repository you are pushing into.

The latter defaults to "unsafe", but if the safety is activated in the repository, forcing from the sending side will not deactivate it. IOW, both ends need to agree to allow the unsafe behavior.

有两个独立的安全机制:

  • 发送端安全性可以被“ git push --force”和/或使用前缀为“ +”的refspec覆盖);

  • 接收端安全性可以被receive.denynonfastworwards您正在推送的存储库的配置变量覆盖 。

后者默认为“不安全”,但如果在存储库中激活安全,则来自发送方的强制不会停用它。IOW,两端都需要同意允许不安全的行为。



As mentioned in the Git FAQ, a possible course of action is:

正如Git FAQ 中所提到的,一个可能的行动方案是:

The most likely reason for this is that you need to pull from the remote first. You can see what changes the remote side has by fetching first and then checking the log. For example,

最可能的原因是您需要先从远程拉取。您可以通过先获取然后检查日志来查看远程端的更改。例如,

 $ git fetch origin
 $ git log master..origin/master

will list all the changes the remote side has that your side doesn't.
If you want a graphical representation, use gitk --left-right master...origin/master.
The arrows to the left are changes you want to push, the arrows to the right are changes on the remote side.

将列出远程端有而你方没有的所有更改。
如果需要图形表示,请使用 gitk --left-right master...origin/master.
左边的箭头是你想要推送的更改,右边的箭头是远程端的更改。

Other solution (which is what you did):

其他解决方案(这就是您所做的):

$ git push origin +branchname

This will force the update. If you don't have permission, then sometimes this will work:

这将强制更新。如果您没有权限,那么有时这会起作用:

$ git push origin :branchname
$ git push origin +branchname

i.e., delete the branch remotely first (this is often permitted), then re-push the "new" (or perhaps rewound) branch.

Be warned that if you rewind branches, others might get into problem when pulling.
There is the chance that they will merge in the branch that they fetched with the new one that you've published, effectively keeping the changes that you are trying to get rid of.
However, it will only be their copies that have the bad revisions. For this reason, rewinding branches is considered mildly antisocial. Nonetheless, it is often appropriate.

即,首先远程删除分支(这通常是允许的),然后重新推送“新”(或者可能重绕)分支。

请注意,如果您倒回树枝,其他人可能会在拉动时遇到问题。
它们有可能合并到它们获取的分支中,并与您发布的新分支合并,从而有效地保留您试图摆脱的更改。
然而,只有他们的副本有错误的修订。因此,倒带分支被认为是轻微的反社会行为。尽管如此,它通常是合适的。