我们的工作流程就是这样。我们有一个叫做dev的分支,我可以在origin/dev找到它。当我们进行更改时,我们创建了一个开发分支:

git checkout -b FixForBug origin/dev

现在我有一个名为FixForBug的分支,它正在跟踪(我认为这是正确的词)origin/dev。因此,如果我做了一个git拉,它将带来来自origin/dev的新变化,这是很棒的。现在,当我完成修复后,我将推到一个名为相同内容的远程分支。

首先,我从origin/dev中删除任何更改,并进行rebase:

git pull --rebase

然后我将更改推到同名的远程分支:

git push origin FixForBug

现在,远程服务器上有一个分支,我可以创建一个pull请求,以批准该更改并将其合并回dev分支。我自己从来没有把任何东西推向原始/开发阶段。我猜这是非常常见的工作流程。

我第一次执行git推送时,它工作得很好,并创建了远程分支。然而,如果我第二次推送(比如在代码审查期间,有人指出了一个问题),我会得到以下错误:

错误:无法推送一些引用 “https://github.mydomain.info/Product/product.git” 提示:更新被拒绝,因为当前分支的尖端落后于远程分支。在再次推送之前整合远程更改(例如:提示:'git pull…')。 详见“git push—help”中的“关于快进的说明”。

然而,如果我做一个git状态,它说我领先于origin/dev 1提交(这是有意义的),如果我遵循提示并运行git pull,它说一切都是最新的。我认为这是因为我推到了一个不同于上游分支的分支。我可以通过运行以下命令来解决这个问题:

git push -f origin FixForBug

在这种情况下,它会将更改推送到远程分支,并表示(强制更新)并且远程分支上的一切看起来都很好。

我的问题:

为什么在这种情况下需要-f ?通常当你强迫做某事时,那是因为你做错了某事,或者至少违反了标准做法。我是否可以这样做,或者它是否会在远程分支中搞砸一些东西,或者为最终必须将我的东西合并到开发中的人制造麻烦?


当前回答

推送被拒绝,因为您当前分支的尖端落后。

当我遇到这样的情况时,我就这样跑:

git push -f origin main

事情就这样完成了。

其他回答

如果您尝试了前面的所有答案,但问题仍然没有解决,那么请确保推送的分支名称是惟一的,并且在远程中不存在。

错误消息可能具有误导性。

-f实际上是必需的,因为它是变基的。每当你做一个rebase,你都需要做一个强制推送,因为远程分支不能快进到你的提交。你总是想要确保你在推送之前做了一个拉,但如果你不喜欢强制推送到master或dev,你可以创建一个新的分支来推送,然后合并或进行PR。

您必须在提交中添加了尚未推送的新文件。检查文件,再次推动该文件,然后尝试拉/推。

它会起作用的。这对我很管用……

如果你想避免使用-f,那么你可以使用just

git pull

而不是

git pull --rebase

非rebase将从origin/dev获取更改,并将它们合并到FixForBug分支中。然后,你就能跑了

git push origin FixForBug

不使用-f。

如果你使用TortoiseGit推送对话

引用来源:https://tortoisegit.org/docs/tortoisegit/tgit-dug-push.html#id692368

known changes - This allows remote repository to accept a safer non-fast-forward push. This can cause the remote repository to lose commits; use it with care. This can prevent from losing unknown changes from other people on the remote. It checks if the server branch points to the same commit as the remote-tracking branch (known changes). If yes, a force push will be performed. Otherwise it will be rejected. Since git does not have remote-tracking tags, tags cannot be overwritten using this option. This passes --force-with-lease option of git push command. unknown changes - This allows remote repository to accept an unsafe non-fast-forward push. This can cause the remote repository to lose commits; use it with care. This does not check any server commits, so it is possible to lose unknown changes on the remote. Use this option with Include Tags to overwrite tags. This passes the traditional --force option of git push command.