当使用git merge将主题分支“B”合并为“A”时,我得到了一些冲突。我知道所有的冲突都可以用B的版本解决。

我知道git合并是我们的。但我想要的是类似git merge -s的东西。

为什么它不存在?如何在与现有git命令冲突合并后实现相同的结果?(git从B中检出所有未合并的文件)

仅仅丢弃分支A中的任何东西(合并提交点到树的B版本)的“解决方案”不是我想要的。


当前回答

这将合并现有的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——strategy=我们的ref-to-be-merge

Git diff——binary ref-to-be-merged | Git apply——reverse——index

Git提交—修改

在我所知道的任何场景中都不会有冲突,您不必创建额外的分支,它就像正常的合并提交一样。

然而,这在子模块中并不适用。

请参阅Junio Hamano被广泛引用的答案:如果您要丢弃已提交的内容,那么只需丢弃提交,或者无论如何要将其排除在主历史记录之外。将来为什么要麻烦每个人从没有提供任何东西的提交中读取提交消息呢?

但有时会有管理要求,或者其他原因。对于那些你真的必须记录没有贡献的提交的情况,你想要:

(编辑:哇,我以前错了吗?这个是可行的。)

git update-ref HEAD $(
        git commit-tree -m 'completely superseding with branchB content' \
                        -p HEAD -p branchB    branchB:
)
git reset --hard

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

Replce <<<<<<< HEAD\n[^•>]+\n==========\n([^•>]+)>>>>>>> .+\n with \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>

这将合并现有的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