当使用git merge将主题分支“B”合并为“A”时,我得到了一些冲突。我知道所有的冲突都可以用B的版本解决。
我知道git合并是我们的。但我想要的是类似git merge -s的东西。
为什么它不存在?如何在与现有git命令冲突合并后实现相同的结果?(git从B中检出所有未合并的文件)
仅仅丢弃分支A中的任何东西(合并提交点到树的B版本)的“解决方案”不是我想要的。
当使用git merge将主题分支“B”合并为“A”时,我得到了一些冲突。我知道所有的冲突都可以用B的版本解决。
我知道git合并是我们的。但我想要的是类似git merge -s的东西。
为什么它不存在?如何在与现有git命令冲突合并后实现相同的结果?(git从B中检出所有未合并的文件)
仅仅丢弃分支A中的任何东西(合并提交点到树的B版本)的“解决方案”不是我想要的。
当前回答
我解决了我的问题
git checkout -m old
git checkout -b new B
git merge -s ours old
其他回答
从现在起,我一直使用Paul Pladijs的答案。我发现,你可以做一个“正常的”合并,发生冲突,所以你这样做
git checkout --theirs <file>
通过使用来自其他分支的修订来解决冲突。如果对每个文件执行此操作,则会得到与预期相同的行为
git merge <branch> -s theirs
无论如何,这种努力比合并策略要多!(这是用git 1.8.0版本测试的)
一个简单而直观的(在我看来)两步方法是
git checkout branchB .
git commit -m "Picked up the content from branchB"
紧随其后的是
git merge -s ours branchB
(将两个分支标记为合并)
唯一的缺点是它不能从当前分支中删除branchB中已删除的文件。然后,两个分支之间的简单区别将显示是否存在这样的文件。
这种方法还可以从之后的修订日志中清楚地看出所做的工作以及意图。
将分支b合并到签出的分支cha的一个可能的和经过测试的解决方案:
# in case branchA is not our current branch
git checkout branchA
# make merge commit but without conflicts!!
# the contents of 'ours' will be discarded later
git merge -s ours branchB
# make temporary branch to merged commit
git branch branchTEMP
# get contents of working tree and index to the one of branchB
git reset --hard branchB
# reset to our merged commit but
# keep contents of working tree and index
git reset --soft branchTEMP
# change the contents of the merged commit
# with the contents of branchB
git commit --amend
# get rid off our temporary branch
git branch -D branchTEMP
# verify that the merge commit contains only contents of branchB
git diff HEAD branchB
为了实现自动化,您可以使用branchA和branchB作为参数将其包装到脚本中。
这个解决方案保留了合并提交的第一个和第二个父节点,就像你期望git merge -s their branchB一样。
如果你在分支A上,请:
git merge -s recursive -X theirs B
在git版本1.7.8上测试
等价于'git merge -s their branchB'(保持父顺序)
在合并之前:
! !确保你处于清洁状态!!
进行合并:
git commit-tree -m "take theirs" -p HEAD -p branchB 'branchB^{tree}'
git reset --hard 36daf519952 # is the output of the prev command
我们做了什么? 我们创建了一个新的提交,它的两个父节点是我们的和他们的,提交的连接是他们的分支b
合并后:
更准确地说应该是:
git commit-tree -m "take theirs" -p HEAD -p 'SOURCE^{commit}' 'SOURCE^{tree}'