Is there a “theirs” version of “git merge -s ours”?

前端 未结 18 1541
傲寒
傲寒 2020-11-22 06:42

When merging topic branch \"B\" into \"A\" using git merge, I get some conflicts. I know all the conflicts can be solved using the version in \"B\".

I a

18条回答
  •  不思量自难忘°
    2020-11-22 07:06

    A possible and tested solution for merging branchB into our checked-out branchA:

    # 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
    

    To automate it you can wrap it into a script using branchA and branchB as arguments.

    This solution preserves the first and second parent of the merge commit, just as you would expect of git merge -s theirs branchB.

提交回复
热议问题