Git 合并在不应该的时候制造冲突

Git merge creating conflicts when it shouldn't

我们有两个分支:masterdev。我们的 ci 在合并到 master.

后部署到生产环境

现在,我们在 dev 中进行开发,并打算在我们希望部署时合并到 master。我们不在 master.

进行开发

当我们从 dev 打开对 master 的拉取请求时,我们向 'conflicts' 致意。这些冲突是我们在开发中所做的更改的列表。解决这些冲突意味着手动检查它们并在每种情况下从 dev 中选择更改。

为什么合并要考虑这些冲突?在合并事件之间没有对 master 中的这些文件进行任何更改。为什么 automerge 不只是将我们在 dev 中所做的所有更改应用到 master 而不是标记它们?

您不应该使用挤压合并来合并两个长期存在的分支。始终使用合并提交。

在这种情况下使用压缩合并的 Git FAQ explains why 是一个问题:

When Git does a normal merge between two branches, it considers exactly three points: the two branches and a third commit, called the merge base, which is usually the common ancestor of the commits. The result of the merge is the sum of the changes between the merge base and each head. When you merge two branches with a regular merge commit, this results in a new commit which will end up as a merge base when they’re merged again, because there is now a new common ancestor. Git doesn’t have to consider changes that occurred before the merge base, so you don’t have to re-resolve any conflicts you resolved before.

When you perform a squash merge, a merge commit isn’t created; instead, the changes from one side are applied as a regular commit to the other side. This means that the merge base for these branches won’t have changed, and so when Git goes to perform its next merge, it considers all of the changes that it considered the last time plus the new changes. That means any conflicts may need to be re-resolved.

无法通过压缩合并来避免此问题。