当使用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

其他回答

将分支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一样。

这将合并现有的baseBranch中的newBranch

git checkout <baseBranch> // this will checkout baseBranch
git merge -s ours <newBranch> // this will simple merge newBranch in baseBranch
git rm -rf . // this will remove all non references files from baseBranch (deleted in newBranch)
git checkout newBranch -- . //this will replace all conflicted files in baseBranch

重新审视这个老问题,因为我刚刚找到了一个兼而有之的解决方案 简短而且——因为它只使用瓷器指令——容易理解。 明确地说,我想回答的问题在标题中提出 问题(实现git merge -s their),而不是问题体。在 换句话说,我想创建一个合并提交,它的树和 第二个父树:

# Start from the branch that is going to receive the merge.
git switch our_branch

# Create the merge commit, albeit with the wrong tree.
git merge -s ours their_branch

# Replace our working tree and our index with their tree.
git restore --source=their_branch --worktree --staged :/

# Put their tree in the merge commit.
git commit --amend

注意:git restore是git中引入的一个相当新的命令 2.23. Git帮助恢复警告

这个命令是实验性的。行为可能会改变。

我用多个版本的git (2.25.1, 2.30.2, 2.31.1, 2.34.1和2.35.1),并按预期工作。

这一个使用了git管道命令读树,但是使得整个工作流程更短。

git checkout <base-branch>

git merge --no-commit -s ours <their-branch>
git read-tree -u --reset <their-branch>
git commit

# Check your work!
git diff <their-branch>

这并不一定能回答最初发帖者的问题,但我在这里遇到的情况是,我已经尝试了合并,但最终发生了冲突。通常我在IDE中管理冲突,但当我无法访问时,可以使用以下REGEX来查找并替换“their”内容的差异:

Replce <<<<<<< HEAD\n[^•>]+\n==========\n([^•>]+)>>>>>>> .+\n with \1

(以防其他人因为和我一样的原因登陆这个页面)。