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

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


当前回答

我使用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

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

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

现在我只需调用

git mergetest <branchname>

看看是否有任何冲突。

用git撤销合并是如此简单,你甚至不应该担心演练:

$ git pull $REMOTE $BRANCH
# uh oh, that wasn't right
$ git reset --hard ORIG_HEAD
# all is right with the world

编辑:正如下面的评论中所指出的,如果你在你的工作目录或暂存区域中有更改,你可能想要在执行上述操作之前隐藏它们(否则它们将在上面的git重置后消失)

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

假设你想测试从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 merge -abort恢复。

但你也可以用幼稚(但缓慢)的方式来做:

rm -Rf /tmp/repository
cp -r repository /tmp/
cd /tmp/repository
git merge ...
...if successful, do the real merge. :)

(注意:仅仅克隆到/tmp不能工作,你需要一个副本,以确保未提交的更改不会冲突)。