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

前端 未结 18 1501
傲寒
傲寒 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 06:46

    This will merge your newBranch in existing baseBranch

    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
    
    0 讨论(0)
  • 2020-11-22 06:56

    See Junio Hamano's widely cited answer: if you're going to discard committed content, just discard the commits, or at any rate keep it out of the main history. Why bother everyone in the future reading commit messages from commits that have nothing to offer?

    But sometimes there are administrative requirements, or perhaps some other reason. For those situations where you really have to record commits that contribute nothing, you want:

    (edit: wow, did I manage to get this wrong before. This one works.)

    git update-ref HEAD $(
            git commit-tree -m 'completely superseding with branchB content' \
                            -p HEAD -p branchB    branchB:
    )
    git reset --hard
    
    0 讨论(0)
  • 2020-11-22 06:56

    A simple and intuitive (in my opinion) two-step way of doing it is

    git checkout branchB .
    git commit -m "Picked up the content from branchB"
    

    followed by

    git merge -s ours branchB
    

    (which marks the two branches as merged)

    The only disadvantage is that it does not remove files which have been deleted in branchB from your current branch. A simple diff between the two branches afterwards will show if there are any such files.

    This approach also makes it clear from the revision log afterwards what was done - and what was intended.

    0 讨论(0)
  • 2020-11-22 06:57

    This one uses a git plumbing command read-tree, but makes for a shorter overall workflow.

    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>
    
    0 讨论(0)
  • 2020-11-22 06:58

    To really properly do a merge which takes only input from the branch you are merging you can do

    git merge --strategy=ours ref-to-be-merged

    git diff --binary ref-to-be-merged | git apply --reverse --index

    git commit --amend

    There will be no conflicts in any scenario I know of, you don't have to make additional branches, and it acts like a normal merge commit.

    This doesn't play nice with submodules however.

    0 讨论(0)
  • 2020-11-22 06:59

    I used the answer from Paul Pladijs since now. I found out, you can do a "normal" merge, conflicts occur, so you do

    git checkout --theirs <file>
    

    to resolve the conflict by using the revision from the other branch. If you do this for each file, you have the same behaviour as you would expect from

    git merge <branch> -s theirs
    

    Anyway, the effort is more than it would be with the merge-strategy! (This was tested with git version 1.8.0)

    0 讨论(0)
提交回复
热议问题