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


当前回答

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

其他回答

这件事刚刚发生在我身上。

昨天我向我们的主人提出了请求。 我的同事今天正在审查它,发现它与我们的主分支不同步,所以为了帮助我,他将master合并到我的分支。 我不知道他会那样做。 然后我在本地合并master,尝试推它,但是失败了。为什么?因为我的同事合并与主人创建了一个额外的提交,我没有在本地!

解决方案:拉下我自己的分支,这样我就能获得额外的提交。然后把它推回我的远程分支。

在我的树枝上,我真的做了:

git pull
git push

为了确保您的本地分支FixForBug没有领先于远程分支FixForBug,在推送之前拉取并合并更改。

git pull origin FixForBug
git push origin FixForBug

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

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

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

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

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

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

6:承诺和推动

我帮助下:

git stash
git pull origin master
git apply
git commit -m "some comment"
git push

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