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.
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.
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.
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.
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
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
git add
it (add
in Git doubles as marking a file resolved)git commit
to complete the merge; orgit merge --abort
and retry the merge with the above-mentioned auto-resolution optionsResolving 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...
test.cpp
to fix the conflicts (they have <<<<
markers)git add test.cpp
git commit
Push development into master
git push --force origin branchA:branchB
This will force a merge and then push
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With