目前尚不清楚您期望的结果是什么,因此在答案和评论中对“正确”的实现方式有些困惑。我尝试给出一个概述,并查看以下三个选项:
尝试合并并使用B解决冲突
这不是 “他们的版本git merge -s ours
”,而是“他们的版本git merge -X ours
”(是的缩写git merge -s recursive -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
请注意,合并现在提交的第一个父对象是from branchB
,只有第二个提交是from branchA
。例如,Gandalf458的答案就是这样做的。
仅使用B的内容并保持正确的父顺序
这是真正的“他们的版本git merge -s ours
”。它的内容与之前的选项相同(即仅来自from 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的回答(不需要临时分支)。