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

我没看到任何类似于,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

其他回答

你可以在看到冲突后执行git merge -abort。

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

假设你想测试从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年多了,没有人会提出这个看似显而易见的解决方案。

我做了一个别名来做这件事,就像一个魅力,我这样做:

 git config --global alias.mergetest '!f(){ git merge --no-commit --no-ff "$1"; git merge --abort; echo "Merge aborted"; };f '

现在我只需调用

git mergetest <branchname>

看看是否有任何冲突。

如果你想从B快进到A,那么你必须确保git log B..A没有显示任何东西,也就是说A没有B没有的东西。但即使B. A有一些东西,你仍然可能能够合并而没有冲突,所以上面说明了两件事:将会有一个快进,因此你不会得到冲突。

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

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