Fixing Git Merge Conflicts: Reference and Examples

Last updated:
Table of Contents

Git version used for examples: 2.x

All examples use origin and master as the remote and branch names, respectively

Pull code, use "theirs" for all conflicts

Pull code and, if their are any conflicts, automatically default to the external (i.e. theirs) version.

$ git pull -s recursive -X theirs origin master

Pull code, use "ours" for all conflicts

Pull code and, if their are any conflicts, automatically default to your own version.

$ git pull -s recursive -X ours origin master

Fix conflict in file using "our" changes

Fix conflicts in a single file, using changes in your local file version (i.e. ours)

$ git checkout --ours my_file_name.txt
$ git add my_file_name.txt

Fix conflict in file using "their" changes

Fix conflicts in a single file, using changes from the other branch version (i.e. theirs)

$ git checkout --theirs my_file_name.txt
$ git add my_file_name.txt

Fix all conflicts using "our" changes

After a merge with conflicts, choose your own version for all conflicts and commit the final version.

(+$|MERGING)$ git checkout --conflict=merge .
(+$|MERGING)$ git checkout --ours .
(+$|MERGING)$ git add .
(+$|MERGING)$ git commit -m "Fixed conflicts using OURS strategy"

Fix all conflicts using "their" changes

After a merge with conflicts, choose their code version for all conflicts and commit the final version.

(+$|MERGING)$ git checkout --conflict=merge .
(+$|MERGING)$ git checkout --theirs .
(+$|MERGING)$ git add .
(+$|MERGING)$ git commit -m "Fixed conflicts using THEIRS strategy"

See also

Dialogue & Discussion