我正在合并一个可能有很多冲突的远程分支机构。我怎么知道它是否会有冲突?

我没看到任何类似于,git合并的预演。


当前回答

作为对已有答案的总结,有两种方法来检查是否存在合并冲突

Git format-patch $(Git merge-base branch1 branch2).——| git apply——3way——check -

注意,在运行上述命令时,当前的分支应该是branch1

另一种方法:

git merge --no-commit branch2
# check the return code here
git merge --abort

其他回答

我知道这在理论上是跑题的,但对于从谷歌搜索到这里的人来说,实际上是非常切题的。

当有疑问时,你可以使用Github界面创建一个pull-request,并检查它是否表明干净的合并是可能的。

我使用git日志来查看从主分支到特性分支上发生了什么变化

git log does_this_branch..contain_this_branch_changes

例如,查看哪些提交在一个已经/没有被合并到master的特性分支中:

git log master..feature_branch

如前所述,传入——no-commit标志,但为了避免快进提交,也传入——no-ff,如下所示:

$ git merge --no-commit --no-ff $BRANCH

检查阶段性的变化:

$ git diff --cached

你可以撤销合并,即使是快进合并:

$ git merge --abort

不完全是那样的。但是您可以使用——no-commit选项,这样它就不会在合并后自动提交结果。通过这种方式,您可以检查合并,如果需要的话,还可以在不破坏提交树的情况下撤消合并。

我很惊讶没有人建议使用补丁。

假设你想测试从your_branch到master的合并(我假设你已经检查了master):

$ git diff master your_branch > your_branch.patch
$ git apply --check your_branch.patch
$ rm your_branch.patch

这样应该可以了。

如果你得到这样的错误

error: patch failed: test.txt:1
error: test.txt: patch does not apply

这意味着补丁并不成功,合并会产生冲突。没有输出意味着补丁是干净的,您可以轻松地合并分支


请注意,这实际上不会改变您的工作树(当然除了创建补丁文件,但您可以安全地删除之后)。在git-apply文档中:

--check
    Instead of applying the patch, see if the patch is applicable to the
    current working tree and/or the index file and detects errors. Turns
    off "apply".

提醒那些比我更聪明/对git更有经验的人:如果我错了,请告诉我,这个方法确实显示出与常规合并不同的行为。奇怪的是,这个问题已经存在8年多了,没有人会提出这个看似显而易见的解决方案。