我们的工作流程就是这样。我们有一个叫做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 ?通常当你强迫做某事时,那是因为你做错了某事,或者至少违反了标准做法。我是否可以这样做,或者它是否会在远程分支中搞砸一些东西,或者为最终必须将我的东西合并到开发中的人制造麻烦?


当前回答

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

git pull

而不是

git pull --rebase

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

git push origin FixForBug

不使用-f。

其他回答

如果你真的担心任何其他方法,这些步骤可以帮助你没有任何困难

1:将您的更改保存在您想要推送的本地分支中

2:重命名你的本地分支为未来的备份

3:从你的远程创建一个同名的分支,它将有所有的更改

4:签出这个新分支作为你的新本地分支

5:在这个分支中进行并保存更改

6:承诺和推动

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

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

如果你使用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.

在这里,当你试图从主分支中重新建立一个dev-feature分支时(Git版本参考>> Git版本Git版本2.37.1 (Apple Git-137.1))

1.Git签出开发功能

签出特性分支

2. git拉

将报头更新为最新提交

3.Git pull -rebase origin master

设置从主回购中提取的rebase标志

4. Git配置拉。变基真 如果调和方法没有设置,因为你有2个分支 5. git拉 成功重基并更新refs/heads/dev-feature** 如果您收到上面的消息,这意味着分支已被重新基于

6. Git push origin dev-feature 将这些更改推到远程

*“当前分支的尖端落后于远程分支的尖端”*意味着远程分支上有您在本地没有的更改。Git告诉您从REMOTE导入新的更改,并将其与您的代码合并,然后将其推送到REMOTE。

您可以使用此命令强制使用本地存储库()对服务器进行更改。远程回购代码将被本地回购代码取代。

git push -f origin master

使用-f标记,您将使用本地回购代码覆盖远程分支代码。