Fixing Git Merge Conflicts: Reference and Examples

Fixing Git Merge Conflicts: Reference and Examples

Last updated:
Table of Contents

Examples use Git version 2

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

Pull code, use "theirs" for all conflicts

Pull code and default to the other version in case there are any conflicts

$ git pull -s recursive -X theirs origin master

Pull code, use "ours" for all conflicts

Pull code and default to this version in case there are any conflicts

$ git pull -s recursive -X ours origin master

Fix conflict in file using "our" changes

Fix conflicts in a single file, using changes in this branch

(+$|MERGING)$ git checkout --ours my_file_name.txt
(+$|MERGING)$ git add my_file_name.txt

Fix conflict in file using "their" changes

Fix conflicts in a single file, using changes in the other branch

(+$|MERGING)$ git checkout --theirs my_file_name.txt
(+$|MERGING)$ 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