[git] Git resolve conflict using --ours/--theirs for all files

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.

This question is related to git conflict resolve merge-conflict-resolution

The answer is


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.


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.


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


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.


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

git merge origin/master --strategy=ours

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

Or for the other way around, see Is there a "theirs" version of "git merge -s ours"?


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


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to conflict

Git resolve conflict using --ours/--theirs for all files Eclipse EGit Checkout conflict with files: - EGit doesn't want to continue How do I implement JQuery.noConflict() ? How can I discard remote changes and mark a file as "resolved"? Android: View.setID(int id) programmatically - how to avoid ID conflicts? How do I manage conflicts with git submodules?

Examples related to resolve

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

Examples related to merge-conflict-resolution

Why does git say "Pull is not possible because you have unmerged files"? Git resolve conflict using --ours/--theirs for all files Cannot checkout, file is unmerged How to interactively (visually) resolve conflicts in SourceTree / git INSERT IF NOT EXISTS ELSE UPDATE? How do I fix a merge conflict due to removal of a file in a branch? Resolving a Git conflict with binary files How to resolve merge conflicts in Git repository?