Using git checkout --theirs
or --ours
is handy for resolving conflicts, but they either take the full "theirs" file or the full "ours" file. It does not merge anything, even in areas where it is easy to merge (i.e. areas where the conflicted file does not show any >>>> and <<<<).
Is it possible to do a kind of checkout --theirs
where there are conflicts but keep merged areas of the file that where correctly merged?
Said differently, I am looking for something that behaves like git merge --strategy-option theirs
but at the scale of a single file (because for some other files I'd like to use other merging strategies).
Here is a minimal example to reproduce my issue:
- We first create a repo with a simple file
a.txt
:
$ git init
$ cat > a.txt
lorem
ipsum
dolor
sit
amet
consectatur
adipiscing
elit
some
area
to
change
$ git add a.txt
$ git commit -m 'A'
- In a branch, we edit the file at multiple location (top and bottom parts):
$ git checkout -b branch
$ cat > a.txt
lorem
ipsum
dolorB
sit
amet
consectatur
adipiscing
elit
B
some
areaB
change
$ git add a.txt
$ git commit -m 'B'
- In the master branch, we edit only one part of the file (top):
$ git checkout master
$ cat > a.txt
lorem
ipsumA
dolorA
sitA
amet
consectatur
adipiscing
elit
some
area
to
change
$ git add a.txt
$ git commit -m 'C'
- When trying to merge, the top part is in conflict but the bottom part could be merged successfully:
$ git merge branch
CONFLICT (content): Merge conflict in a.txt
$ cat a.txt
lorem
<<<<<<< HEAD
ipsumA
dolorA
sitA
=======
ipsum
dolorB
sit
>>>>>>> branch
amet
consectatur
adipiscing
elit
B
some
areaB
change
What I would like to have is a merged a.txt
where each time there is a conflict the HEAD block is used (master) but still merge areas where there is no conflict.
Neither git checkout --theirs
nor git checkout --ours
works here:
$ git checkout --theirs a.txt
$ cat a.txt
lorem
ipsum
dolorB <--- NOPE!
sit
amet
consectatur
adipiscing
elit
B
some
areaB
change
$ git checkout --ours a.txt
$ cat a.txt
lorem
ipsumA
dolorA
sitA
amet
consectatur
adipiscing
elit
some
area <--- NOPE!
to
change
What I would like to have is:
lorem
ipsumA
dolorA
sitA
amet
consectatur
adipiscing
elit
B
some
areaB
change