当使用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版本)的“解决方案”不是我想要的。
当前回答
将分支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一样。
其他回答
当合并主题分支“B”在“A”使用git合并,我得到一些冲突。我知道所有的冲突都可以用“B”中的版本来解决。 我知道git合并是我们的。我想要的是git merge >-s their。
我假设你从master创建了一个分支,现在想合并回master,覆盖master中的任何旧东西。当我看到这篇文章时,这正是我想做的。
做你想做的,除了先把一个分支合并到另一个分支。我就这么做了,效果很好。
git checkout Branch
git merge master -s ours
然后,签出master并合并你的分支(现在会很顺利):
git checkout master
git merge Branch
请参阅Junio Hamano被广泛引用的答案:如果您要丢弃已提交的内容,那么只需丢弃提交,或者无论如何要将其排除在主历史记录之外。将来为什么要麻烦每个人从没有提供任何东西的提交中读取提交消息呢?
但有时会有管理要求,或者其他原因。对于那些你真的必须记录没有贡献的提交的情况,你想要:
(编辑:哇,我以前错了吗?这个是可行的。)
git update-ref HEAD $(
git commit-tree -m 'completely superseding with branchB content' \
-p HEAD -p branchB branchB:
)
git reset --hard
将分支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一样。
这个答案是由Paul Pladijs给出的。为了方便起见,我只是把他的命令做了一个git别名。
编辑你的.gitconfig并添加以下内容:
[alias]
mergetheirs = "!git merge -s ours \"$1\" && git branch temp_THEIRS && git reset --hard \"$1\" && git reset --soft temp_THEIRS && git commit --amend && git branch -D temp_THEIRS"
然后你可以“git merge -s their A”通过运行:
git checkout B (optional, just making sure we're on branch B)
git mergetheirs A
我最近需要为两个共享共同历史的独立存储库执行此操作。我从:
Org/repository1主 Org/repository2主
我希望将repository2 master的所有更改应用到repository1 master,接受repository2所做的所有更改。在git的术语中,这应该是一个称为-s their的策略,但它不存在。要小心,因为-X their的名字就像你想要的那样,但它不是一样的(它甚至在手册页中这样说)。
我解决这个问题的方法是到repository2并创建一个新的分支repo1-merge。在那个分支中,我运行git拉git@gitlab.com:Org/repository1 -s ours,它合并得很好,没有问题。然后我把它推到遥控器上。
然后回到repository1并创建一个新的分支rep2 -merge。在该分支中,我运行git拉git@gitlab.com:Org/repository2 repo1-merge,它将与问题一起完成。
最后,您可能需要在repository1中发出合并请求,使其成为新的主节点,或者只是将其作为一个分支。