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.

- 1,781
- 3
- 12
- 5
-
9Does `git checkout --ours -- .` work? The `.` means the current directory, when applied from the working directory root, so it basically means the entire working directory. Not sure if that will work with the `--ours` flag though, and I'm not sure how it will handle deleted or renamed file conflicts. – Jul 14 '14 at 19:08
-
@Cupcake I've used it successfully. Maybe that should be an answer? – Pedro Gimeno Oct 13 '15 at 17:37
-
If someone is looking for an answer to do this in Visual Studio, take a look at this: https://stackoverflow.com/a/74605102/8644294 – Ash K Nov 28 '22 at 18:55
6 Answers
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

- 2,658
- 2
- 25
- 41
-
21Instead of grepping, I think it’s also possible to use [`git diff --name-only --diff-filter=U`](http://stackoverflow.com/a/10874862/559913). – Thai Nov 16 '15 at 03:22
-
11Also `git status | grep both | awk '{print $3}' | xargs git checkout --[theirs|ours]`. Quicker than grepping if you have a large project. – joshbodily Dec 11 '15 at 17:09
-
1@joshbodily Nice. I don't have projects so huge that I would notice a difference, but I can see how it would be much faster especially if few files have changed compared to the total number in the directory----which _should_ be the case for normal commits. – Dmitri Dec 11 '15 at 22:08
-
1Note that this does not work for rename/rename conflicts, e.g., `CONFLICT (rename/rename): Rename "a.txt"->"c.txt" in branch "HEAD" rename "a.txt"->"b.txt" in "master"` – Borek Bernard Sep 29 '16 at 12:48
-
1Yet another option: `git status --porcelain | egrep '^UU' | cut -d ' ' -f 2 |xargs git checkout --[theirs|ours]`. – Zitrax Mar 16 '17 at 15:47
-
-
@AbdulHannan: You can escape the spaces in your filenames with a backslash: `file\ name\ with\ spaces.txt` Or, enclose all pathnames in double quotes: https://unix.stackexchange.com/questions/131766/why-does-my-shell-script-choke-on-whitespace-or-other-special-characters – Dmitri Aug 23 '17 at 18:55
-
@dimitri. Thats clear to me but how above answer can be made to work with directories containing spaces? – Abdul Hannan Aug 23 '17 at 19:09
-
@AbdulHannan It shouldn't make a difference: grep should spit out an argument into the pipe that xargs + git can understand. At least it does on a UN*X system. Are you trying this on Windows? – Dmitri Aug 23 '17 at 19:15
-
-
1I had filenames with spaces and single quotes and I had to use the -z option on git diff in conjunction with the -0 option on xargs to get it to work. `git diff --name-only --diff-filter=U -z | xargs -0 git checkout --theirs` – Sean Ferons Sep 01 '20 at 17:03
-
For big projects this is really slow, it would also add jpegs and other files that didn't have conflicts. @Peter 's answer is the way to go. – Declan McKenna Mar 22 '22 at 16:35
You can -Xours
or -Xtheirs
with git merge
as well. So:
- abort the current merge (for instance with
git reset --hard HEAD
) - merge using the strategy you prefer (
git merge -Xours
orgit 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.

- 7,101
- 5
- 35
- 54
-
Thanks, didn't know they have '--ours' and '--theirs' for merge. It seems that for merge they are actually not part of '-s' strategy but part of '-X' recursive options. '-s' version of ours actually just replaces all files without merging and theirs doesn't exist. – exe163 Jul 16 '14 at 21:33
-
There is no `--theirs` or `--ours`-Option to git v1.9.4. An approach would be `git merge -s recursive -Xtheirs BRANCH`. – fbmd Jun 04 '15 at 12:49
-
`--theirs` and `--ours` are not available neither with git `2.2.0`. Either my answer was not precise or they were available in older git version (this answer is pretty old in IT epoch). The right approach is with `-X`. I update accordingly – ThanksForAllTheFish Jun 04 '15 at 14:34
-
As of Version 2.4.0 --ours and --theirs are still very much available http://git-scm.com/docs/git-checkout – Dmitri Jun 27 '15 at 00:04
-
Tried this, and got "automatic merge failed, fix conflicts". This command clearly doesn't work as intended, and I recommend avoiding it - too buggy. – Adam May 16 '17 at 20:54
-
Also works when `rebase`ing, although the identities of `ours` and `theirs` is switched (`Xours` means keep the changes from the branch being rebased **onto**). – ijoseph Mar 01 '19 at 21:42
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.

- 12,458
- 3
- 39
- 44
-
5
-
5
-
I'm using git version 2.16.2.windows.1 and it works perfectly for me. Thanks! – Pankwood Jun 22 '18 at 15:03
-
3
-
6`$ git checkout --ours` gives: `fatal: '--ours/--theirs' cannot be used with switching branches`. `git version 2.31.1 hub version 2.14.2` – Vladimir Vukanac Jul 27 '21 at 10:35
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.

- 12,274
- 9
- 71
- 86
-
5I think this is a better answer than the top-voted one because (1) it applies to all files, not just the working directory; and (2) it won't have any brittleness around grepping for conflict markers. The 'git diff' command here lists all unmerged paths, which is exactly what we want to checkout. – bchurchill Mar 31 '18 at 21:04
-
Nice! I was always concerned about the reliability of the conflict markers grep – 00-BBB Jan 15 '20 at 12:08
-
I pieced exactly this together from above answers. Agreed this is the cleanest + most performant (the `grep` option is really a non-starter for larger projects, esp JS projects with sizeable `node_modules` folders). I ended up adding these aliases to my `.bashrc`: ``` alias git-co-ours="git diff --name-only --diff-filter=U | xargs git checkout --ours" alias git-co-theirs="git diff --name-only --diff-filter=U | xargs git checkout --theirs" ``` – ericsoco Nov 16 '20 at 23:26
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 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

- 12,169
- 4
- 59
- 75
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

- 5,470
- 2
- 37
- 71