Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

merge

github

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 important to to perform this merge so the person submitting the pr gets credit for it.

# Step 1: From your project repository, check out a new branch and test the changes.
git checkout -b droark-master master
git pull https://github.com/droark/cryptopp.git master

# Step 2: Merge the changes and update on GitHub.
git checkout master
git merge --no-ff droark-master
git push origin master

I know how to fix the one line in the one conflicting file. What I don't know how to do is make Git perform the merge and stop complaining about broken index files.

How do I make Git perform the merge, ensure the person who provided the pull request gets credit for it, and stop breaking index files?


I tried to repair the merge with Git merge errors. One set of errors turns into another set of errors, ad infinitum. I also tried resetting the problem file according to Ignore files during merge with plans to copy/paste the one line needed, but the broken index persists.

This has turned into a complete waste of time, and I am no longer interested in trying to do it Git's way since it wastes so much time. Now I simply want Git to perform the merge and stop breaking index files.


Here is the output produced when merging using github's instructions:

$ git pull https://github.com/droark/cryptopp.git master
From https://github.com/droark/cryptopp
 * branch            master     -> FETCH_HEAD
Auto-merging validate.h
Auto-merging validat2.cpp
Auto-merging validat1.cpp
Auto-merging test.cpp
CONFLICT (content): Merge conflict in test.cpp
Auto-merging pubkey.h
Automatic merge failed; fix conflicts and then commit the result.
like image 956
jww Avatar asked Apr 24 '16 06:04

jww


People also ask

What happens if you get a conflict during a merge?

A merge conflict is an event that occurs when Git is unable to automatically resolve differences in code between two commits. When all the changes in the code occur on different lines or in different files, Git will successfully merge commits without your help.

How do I resolve merge conflicts in git merge?

Git commands that can help resolve merge conflicts Passing the --merge argument to the git log command will produce a log with a list of commits that conflict between the merging branches. diff helps find differences between states of a repository/files.

What is one way you can lower the chances of merge conflicts occurring?

In order to avoid a merge conflict, all changes must be on different lines, or in different files, which makes the merge simple for computers to resolve. In other words, if a change introduces any ambiguity even at a single line of code an automatic merging is canceled and the whole process must be finished manually.


5 Answers

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
like image 77
Anthony E Avatar answered Oct 02 '22 08:10

Anthony E


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
like image 38
ivan_pozdeev Avatar answered Oct 02 '22 07:10

ivan_pozdeev


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
like image 33
Schwern Avatar answered Oct 02 '22 08:10

Schwern


Push development into master

git push --force origin branchA:branchB

This will force a merge and then push

like image 2
ShadowCore Avatar answered Oct 02 '22 07:10

ShadowCore


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

like image 2
john Avatar answered Oct 02 '22 09:10

john