How to force a merge to succeed when there are conflicts?

前端 未结 5 1473
挽巷
挽巷 2020-12-15 04:36

I\'m trying to merge a pull request that has one conflict in one file (see below). The instructions for merging the pull request are provided by github are as follows. Its i

相关标签:
5条回答
  • 2020-12-15 04:55

    There's no way around resolving conflicts, that's how revision control works (if Alice says "a" and Bob says "b", how should Git know which one is correct unless you tell it?). All you can do is direct git to resolve them itself when merging in one of several possible ways, e.g.

    git merge -s recursive -X theirs <branch>
    

    (-s recursive is the default when there's only one <branch> so you can omit it here)

    Now that you already have a conflict in your tree, you either

    • follow the manual resolution route
      • edit the file to your heart's content
      • git add it (add in Git doubles as marking a file resolved)
      • git commit to complete the merge; or
    • restore the pre-merge state with git merge --abort and retry the merge with the above-mentioned auto-resolution options
    0 讨论(0)
  • 2020-12-15 04:56

    Resolving a conflict is just like dealing with any other work in progress. All changes must be staged (git add) and then committed. Files which have successfully auto merged are already staged. Conflicted files are not.

    Edit the conflicted files to your satisfaction, stage them (git add), and when that is all done, git commit.

    In your case specifically...

    • Edit test.cpp to fix the conflicts (they have <<<< markers)
    • git add test.cpp
    • Run your tests to make sure everything is ok.
    • git commit
    0 讨论(0)
  • 2020-12-15 04:57

    Push development into master

    git push --force origin branchA:branchB
    

    This will force a merge and then push

    0 讨论(0)
  • 2020-12-15 05:05

    If you know the changes in the current working branch is what you want, you can simply add ours flag to a git merge.

    git merge -s ours master
    

    This effectively ignores all other branch changes and guarantees the merge output is that of the current working branch.

    More info and strategies here : https://www.atlassian.com/git/tutorials/using-branches/merge-strategy

    0 讨论(0)
  • 2020-12-15 05:10

    There's no way to merge without resolving conflicts. Otherwise, how would git know what to merge? You can, however, checkout the version from either branch you're merging using git checkout --ours <filepath> or git checkout --theirs <filepath>. Here's an example:

    Suppose you're on the master branch merging in staging:

    git checkout master
    git merge staging
    

    And git shows a bunch of conflicts:

    ...
    CONFLICT: Readme.md
    ...
    

    If you want to keep the version of Readme.md that's on master, then you would run:

    git checkout --ours Readme.md
    

    Note that since you're on master --ours refers to "this" branch, i.e. master.

    Now, you can simply add it to the index to mark it as resolved:

    git add Readme.md
    

    This effectively ignores any changes to Readme.md on the staging branch.

    You can repeat this process for each file you want to omit from the merge. When you're done, commit as you normally would:

    git commit -m "whatever..."
    

    In order to repeat it for all files with conflicts you can do

    for f in $(git diff --name-only --diff-filter=U | cat); do
       echo "Resolve conflict in $f ..."
       git checkout --theirs $f
    done
    
    0 讨论(0)
提交回复
热议问题