How to resolve ALL conflicts using HEAD, with any mergetool
Asked Answered
O

3

41

So for some reason I'm getting a lot of conflicts with a new merged hotfix. The file that was actually [manually] changed has no conflict. All the conflicts are in files that were untouched during the fix and apparently its an issue with whitespaces. I'll try to figure that problem later but now I need to merge the hotfix and deploy.

How can I resolve ALL conflicts to use the HEAD version? I don't want to go file by file. Yes, I know its a bad practice but the conflicts are all whitespaces and I know HEAD is correct — passing all tests and running fine in production.

Any ideas?

I'm using OSX.

Offutt answered 26/11, 2013 at 22:21 Comment(0)
S
82
git merge -Xours origin/master

will do a merge with origin/master (the same thing that git pull origin master does) and will resolve any conflicts by taking the versions from your local branch.

If you're already part-way through the bad merge, you can reset everything to the head first with git reset --hard HEAD.

In that case, you should do

git reset --hard HEAD
git merge -Xours origin/master

And that should fix your problem!

(also worth mentioning, -Xtheirs will do the same thing, but take the upstream version in any conflicts.)


Also, most likely the conflicts are because the upstream version is using windows-style line endings, and whatever program you edited the files in on your local machine is using mac-style or linux-style line endings.

There are options you can set in git to always commit windows-style or linux-style line endings, but always checkout mac-style or linux-style in your working directory.

See this link for more info: https://help.github.com/articles/dealing-with-line-endings

Spathose answered 26/11, 2013 at 22:28 Comment(4)
:) no problem! are you talking about the line endings or the -Xours? I had to look a WHILE before I found -X..Spathose
the -X. I'm going to take a look later about the reason for conflicts. I think it's more a problem with tabs/spaces and having different configs in sublime and vim.Offutt
-Xours doesn't seem to work with deleted files- gives the same error message as if I didn't specify "ours". Any way to convince to take my (deleted) version? Thanks.Unless
@inger: After the merge, if you're still seeing errors, you can run git checkout HEAD -- ., or replace . with the specific deleted filename, to take "our" version of the files. For completeness, git checkout origin/master -- . will take "theirs"! :) Does that help?Spathose
S
16

If you have conflict on your local branch you can simply run these commands:

git checkout --conflict=merge .
git checkout --ours .

To solve conflicts using your local branch.

Silva answered 13/9, 2018 at 12:36 Comment(1)
What is the different between the two commands?Argus
B
6

I would:

$ git checkout master   # or where ever you want to merge the hotfix into
$ git merge --no-commit -Xours <hotfix-branch>
$ git status    # make sure the only change is the file you wanted
$ git diff      # make sure they changes are what you wanted
$ git commit -m "<your merge message"

This will use the default recursive strategy pull in any non-conflicting files, but it will resolve any conflicting files by using simply using the master/HEAD version. The docs:

$ git merge --help
....

   recursive
       ... This is the default merge strategy when pulling or merging one branch.

       The recursive strategy can take the following options:

       ours
           This option forces conflicting hunks to be auto-resolved cleanly
           by favoring our version. Changes from the other tree that do not
           conflict with our side are reflected to the merge result. ....
Betimes answered 26/11, 2013 at 22:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.