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

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

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

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


当前回答

旧版本的git允许你使用“their”合并策略:

git pull --strategy=theirs remote_branch

但是这已经被删除了,正如Junio Hamano (Git维护者)在这条消息中解释的那样。正如链接中提到的,相反,你会这样做:

git fetch origin
git reset --hard origin

但是要注意,这与实际的合并不同。你的解决方案可能就是你真正在寻找的选项。

其他回答

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

这个答案是由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

旧版本的git允许你使用“their”合并策略:

git pull --strategy=theirs remote_branch

但是这已经被删除了,正如Junio Hamano (Git维护者)在这条消息中解释的那样。正如链接中提到的,相反,你会这样做:

git fetch origin
git reset --hard origin

但是要注意,这与实际的合并不同。你的解决方案可能就是你真正在寻找的选项。

将分支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 merge——strategy=我们的ref-to-be-merge

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

Git提交—修改

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

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