当使用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”在“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
其他回答
将分支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一样。
我解决了我的问题
git checkout -m old
git checkout -b new B
git merge -s ours old
请参阅Junio Hamano被广泛引用的答案:如果您要丢弃已提交的内容,那么只需丢弃提交,或者无论如何要将其排除在主历史记录之外。将来为什么要麻烦每个人从没有提供任何东西的提交中读取提交消息呢?
但有时会有管理要求,或者其他原因。对于那些你真的必须记录没有贡献的提交的情况,你想要:
(编辑:哇,我以前错了吗?这个是可行的。)
git update-ref HEAD $(
git commit-tree -m 'completely superseding with branchB content' \
-p HEAD -p branchB branchB:
)
git reset --hard
你想要的结果是什么并不完全清楚,所以在答案和他们的评论中有一些关于“正确”方法的困惑。我试着给出一个概述,并看到以下三个选项:
尝试合并并使用B来处理冲突
这不是“他们版本的git merge -s ours”,而是“他们版本的git merge -X ours”(这是git merge -s递归-X ours的缩写):
git checkout branchA
# also uses -s recursive implicitly
git merge -X theirs branchB
这就是Alan W. Smith的回答所做的。
只使用B的内容
这将为两个分支创建一个合并提交,但会丢弃来自branchA的所有更改,只保留来自branchB的内容。
# Get the content you want to keep.
# If you want to keep branchB at the current commit, you can add --detached,
# else it will be advanced to the merge commit in the next step.
git checkout branchB
# Do the merge an keep current (our) content from branchB we just checked out.
git merge -s ours branchA
# Set branchA to current commit and check it out.
git checkout -B branchA
请注意,合并提交的第一个父节点是来自branchB的,只有第二个是从branchA提交的。这就是例如Gandalf458的答案所做的。
只使用B的内容,并保持正确的父顺序
这是真正的“他们版本的git合并-我们的”。它的内容与之前的选项相同(即只有来自branchB的内容),但父元素的顺序是正确的,即第一个父元素来自branchA,第二个来自branchB。
git checkout branchA
# Do a merge commit. The content of this commit does not matter,
# so use a strategy that never fails.
# Note: This advances branchA.
git merge -s ours branchB
# Change working tree and index to desired content.
# --detach ensures branchB will not move when doing the reset in the next step.
git checkout --detach branchB
# Move HEAD to branchA without changing contents of working tree and index.
git reset --soft branchA
# 'attach' HEAD to branchA.
# This ensures branchA will move when doing 'commit --amend'.
git checkout branchA
# Change content of merge commit to current index (i.e. content of branchB).
git commit --amend -C HEAD
这就是Paul Pladijs的答案所做的(不需要一个临时分支)。
特殊情况
如果branchB的提交是branchA的祖先,git merge将不起作用(它只会退出,并显示“Already up to date.”这样的消息)。
在这种或其他类似/高级的情况下,可以使用低级命令git提交树。
从现在起,我一直使用Paul Pladijs的答案。我发现,你可以做一个“正常的”合并,发生冲突,所以你这样做
git checkout --theirs <file>
通过使用来自其他分支的修订来解决冲突。如果对每个文件执行此操作,则会得到与预期相同的行为
git merge <branch> -s theirs
无论如何,这种努力比合并策略要多!(这是用git 1.8.0版本测试的)