gpt4 book ai didi

Git 非快进被拒绝

转载 作者:IT王子 更新时间:2023-10-29 01:25:33 24 4
gpt4 key购买 nike

我觉得这个问题已经被问过很多次了,但解决方案通常是“我删除了目录并重新检查了我的工作”。我进行了提交和推送,但意识到我在提交消息中引用了错误的票号。所以我快速查看了 SO solution并最终在终端中输入以下内容:

$ git reset --soft HEAD^
$ git commit -m "... correct message ..."

唯一的问题是我收到以下错误消息:

To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again. See the 'Note about
fast-forwards' section of 'git push --help' for details.

我正在使用 git-flow 模型并在开发分支上工作。我怎样才能将东西 merge 回去让 git 再次开心?

最佳答案

如果您将提交推送到服务器,然后在本地重写该提交(使用 git resetgit rebasegit filter-branch 或任何其他历史操作),然后将重写的提交推送回服务器,你会搞砸其他 pull 过的人。这是一个例子;假设你提交了 A,并将其推送到服务器。

-*-*-A <-- master-*-*-A <-- origin/master

Now you decide to rewrite A, in the way you mentioned, resetting and re-committing. Note that this leaves a dangling commit, A, which will eventually be garbage collected as it is not reachable.

-*-*-A    \     A' <-- master-*-*-A  <-- origin/master

If someone else, let's say Fred, pulls down master from the server while you're doing this, they will have a reference to A, which they might start working from:

-*-*-A' <-- master-*-*-A  <-- origin/master-*-*-A-B <-- fred/master

Now if you were able to push your A' to origin/master, which would create a non-fast-forward, it wouldn't have A in its history. So if Fred tried to pull again, he'd suddenly have to merge, and would re-introduce the A commit:

-*-*-A' <-- master-*-*-A  <-- origin/master-*-*-A-B-\     \     * <-- fred/master     A'--/

If Fred happens to notice this, then he could do a rebase, which would prevent commit A from reappearing again. But he'd have to notice this, and remember to do this; and if you have more than one person who pulled A down, they would all have to rebase in order to avoid getting the extra A commit in the tree.

So, it's generally not a good idea to change history on a repo that other people pull from. If, however, you happen to know that no one else is pulling from that repo (for instance, it's your own private repo, or you only have one other developer working on the project who you can coordinate with easily), then you can forcibly update by running:

git push -f

git push origin +master

这些都将忽略对非快进推送的检查,并将服务器上的内容更新为新的 A' 修订版,放弃 A 修订版,因此它最终将被垃圾收集。

receive.denyNonFastForwards 配置选项可能会完全禁用强制推送。默认情况下,此选项在共享存储库上启用。在那种情况下,如果你真的非常想强制推送,最好的选择是删除分支并重新创建它,使用 git push origin :master; git push origin master:master.但是,启用 denyNonFastForwards 选项是有原因的,如上所述;在共享存储库上,这意味着现在使用它的每个人都需要确保他们 rebase 到新的历史。

在共享存储库上,通常最好将新提交推送到最上面以解决您遇到的任何问题;你可以使用 git revert生成将撤消先前提交更改的提交。

关于Git 非快进被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5667476/

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