git 可以告诉我合并是否会在没有实际合并的情况下发生冲突吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6335717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can git tell me if a merge will conflict without actually merging?
提问by Penz
Is it possible to know if a merge will conflict or not, without touching the working tree? I don't want to touch the working tree because I don't want to have it checked-out. That would take a long time if I want to see this information for several branches.
是否可以在不触及工作树的情况下知道合并是否会发生冲突?我不想触摸工作树,因为我不想检查它。如果我想查看多个分支的此信息,那将需要很长时间。
采纳答案by Ian Robinson
I'm assuming you just want to find out how much trouble you're getting yourself into prior to actually attempting the merge...and resetting to the last commit after a failed merge is relatively easy so I wouldn't be surprised if that is the intended approach.
我假设您只是想在实际尝试合并之前找出自己遇到了多少麻烦……并且在合并失败后重置为最后一次提交相对容易,因此如果那样我不会感到惊讶是预期的方法。
That said, if you really don't want to touch your existing files in the working tree - you could create a patch and test it against the target branch. This also has the benefit of showing exactly what changes were made to which files - just open up the patch file in a text editor.
也就是说,如果您真的不想触及工作树中的现有文件 - 您可以创建一个补丁并针对目标分支对其进行测试。这还有一个好处是可以准确显示对哪些文件进行了哪些更改 - 只需在文本编辑器中打开补丁文件即可。
git checkout -b mycrazybranch
[change some stuff...]
git add .
git commit -m "changed some stuff"
git format-patch master --stdout > crazy.patch
git checkout master
git apply crazy.patch --check
[all good! cleanup...]
rm crazy.patch
As you can see, this will create a patch file, you can then test it with --check and see if there are any errors, then remove the patch file.
如您所见,这将创建一个补丁文件,然后您可以使用 --check 对其进行测试,看看是否有任何错误,然后删除补丁文件。
回答by manojlds
You can do git merge --abort
after seeing that there are conflicts.
你可以git merge --abort
看到有冲突之后。
回答by Congbin Guo
As a summary of existed answers, there are two way to check if there would be merge conflicts
作为现有答案的总结,有两种方法可以检查是否会出现合并冲突
git format-patch $(git merge-base branch1 branch2)..branch2 --stdout | git apply --3way --check -
git format-patch $(git merge-base branch1 branch2)..branch2 --stdout | git apply --3way --check -
Note, your current branch should be branch1
when you run above command
请注意,branch1
当您运行上述命令时,您当前的分支应该是
Another way:
其它的办法:
git merge --no-commit branch2
# check the return code here
git merge --abort
回答by Alexandre
Not exactly like that. But you can use the --no-commit option, so it does not automatically commit the result after the merge. In this way you can inspect, and if desired, to undo the merge without messing with the commit tree.
不完全是那样。但是您可以使用 --no-commit 选项,因此它不会在合并后自动提交结果。通过这种方式,您可以检查并在需要时撤消合并,而不会弄乱提交树。