gpt4 book ai didi

git - 为什么是 "git push helloworld +master:master"而不是 "git push helloworld"?

转载 作者:太空狗 更新时间:2023-10-29 12:44:24 28 4
gpt4 key购买 nike

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

$ git push helloworld

但我得到了这个:

To git-smichaels@free5.projectlocker.com:helloworld.git
! [rejected] HEAD -> master (non-fast forward) error:
failed to push some refs to 'git-smichaels@free5.projectlocker.com:helloworld
git'

所以我找到了another StackOverflow question关于“修改后的提交”并尝试了那里的建议但实际上不知道它是否对我有帮助:

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

成功了!

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

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

最佳答案

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

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

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

注意:如果其他人已经克隆了您的存储库,这可能会很糟糕,因为他们将不再能够在不发生冲突的情况下 pull 您的 master 分支。
另见 SO answer for more .


注:如前所述by 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.denynonfastworwards of 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 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.

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

$ 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.

关于git - 为什么是 "git push helloworld +master:master"而不是 "git push helloworld"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1475665/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com