Git resolve conflict using --ours/--theirs for all files

GitConflictResolveMerge Conflict-Resolution

Git Problem Overview


Is there a way to resolve conflict for all files using checkout --ours and --theirs? I know that you can do it for individual files but couldn't find a way to do it for all.

Git Solutions


Solution 1 - Git

Just grep through the working directory and send the output through the xargs command:

grep -lr '<<<<<<<' . | xargs git checkout --ours

or

grep -lr '<<<<<<<' . | xargs git checkout --theirs

How this works: grep will search through every file in the current directory (the .) and subdirectories recursively (the -r flag) looking for conflict markers (the string '<<<<<<<')

the -l or --files-with-matches flag causes grep to output only the filename where the string was found. Scanning stops after first match, so each matched file is only output once.

The matched file names are then piped to xargs, a utility that breaks up the piped input stream into individual arguments for git checkout --ours or --theirs

More at this link.

Since it would be very inconvenient to have to type this every time at the command line, if you do find yourself using it a lot, it might not be a bad idea to create an alias for your shell of choice: Bash is the usual one.

This method should work through at least Git versions 2.4.x

Solution 2 - Git

You can -Xours or -Xtheirs with git merge as well. So:

  1. abort the current merge (for instance with git reset --hard HEAD)
  2. merge using the strategy you prefer (git merge -Xours or git merge -Xtheirs)

DISCLAIMER: of course you can choose only one option, either -Xours or -Xtheirs, do use different strategy you should of course go file by file.

I do not know if there is a way for checkout, but I do not honestly think it is terribly useful: selecting the strategy with the checkout command is useful if you want different solutions for different files, otherwise just go for the merge strategy approach.

Solution 3 - Git

git checkout --[ours/theirs] . will do what you want, as long as you're at the root of all conflicts. ours/theirs only affects unmerged files so you shouldn't have to grep/find/etc conflicts specifically.

Solution 4 - Git

git diff --name-only --diff-filter=U | xargs git checkout --theirs

Seems to do the job. Note that you have to be cd'ed to the root directory of the git repo to achieve this.

Solution 5 - Git

Scenario 1

In case anyone else is looking to simply overwrite everything in one branch with the contents of another branch (e.g. master), there's an easier way:

git merge origin/master --strategy=ours

Thanks to https://stackoverflow.com/a/1295232/560114

Scenario 2

For the other way around, see https://stackoverflow.com/questions/173919/is-there-a-theirs-version-of-git-merge-s-ours

Update

For scenario 2, the linked answer recommends using:

git checkout branchA
git merge -X theirs branchB

I have found that this is not exactly the reverse of --strategy=ours and you might still get merge conflicts (and in some cases code from the other branch being kept that you actually wanted to remove). So if you want a solution to truly do the opposite of git merge other-branch --strategy=ours, the best way is to do it in two steps (two merges). Suppose your goal is to replace branch-a with the contents of branch-b. Then the first step would be to do this:

git checkout branch-b
git fetch branch-a
git merge branch-a --strategy=ours

Now branch-b is ready to merge into branch-a without conflicts. At this point, if you're using something like Github, you could raise a PR to merge branch-b into branch-a. Or if no peer review is required, you could do the merge directly:

git checkout branch-a
git merge branch-b

# Cleanup - delete branch-b (if desired)
git branch -d branch-b
git push origin --delete branch-b

Solution 6 - Git

function gitcheckoutall() {
    git diff --name-only --diff-filter=U | sed 's/^/"/;s/$/"/' | xargs git checkout --$1
}

I've added this function in .zshrc file.

Use them this way: gitcheckoutall theirs or gitcheckoutall ours

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionexe163View Question on Stackoverflow
Solution 1 - GitDmitriView Answer on Stackoverflow
Solution 2 - GitThanksForAllTheFishView Answer on Stackoverflow
Solution 3 - GitCory PetoskyView Answer on Stackoverflow
Solution 4 - GitPeterView Answer on Stackoverflow
Solution 5 - GitMatt BrowneView Answer on Stackoverflow
Solution 6 - GitiWheelBuyView Answer on Stackoverflow