一个可能的和经过测试的解决方案,用于将分支B合并到我们的签出分支A中:# in case branchA is not our current branchgit checkout branchA# make merge commit but without conflicts!!# the contents of 'ours' will be discarded latergit merge -s ours branchB # make temporary branch to merged commitgit branch branchTEMP # get contents of working tree and index to the one of branchBgit reset --hard branchB# reset to our merged commit but # keep contents of working tree and indexgit reset --soft branchTEMP# change the contents of the merged commit# with the contents of branchBgit commit --amend# get rid off our temporary branchgit branch -D branchTEMP# verify that the merge commit contains only contents of branchBgit diff HEAD branchB要使其自动化,可以使用分支A和分支B作为参数将其包装到脚本中。此解决方案保留合并提交的第一个和第二个父级,正如您所期望的git merge -s theirs branchB.