ipsec
ipsec

Reputation: 680

How to keep both files from a “both added” merge conflict?

I want to merge a branch which added a file that I have added as well:

Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both added:      file

I want to resolve this conflict by renaming my version of file to other_file while keeping the version of the other branch.

My solution would be:

git checkout --ours file
mv file other_file
git checkout --theirs file
git add file other_file

Is there an easier or more direct solution?

Upvotes: 3

Views: 2734

Answers (1)

torek
torek

Reputation: 489828

I recreated this kind of conflict:

$ git merge b1
CONFLICT (add/add): Merge conflict in newfile
Auto-merging newfile
Automatic merge failed; fix conflicts and then commit the result.

and indeed, there's Git's attempt to merge the files in the work-tree:

$ cat newfile   
<<<<<<< HEAD
different contents - add a file
||||||| merged common ancestors
=======
add a file
>>>>>>> b1

Your method will work fine. This one is not much shorter, and has potential glitches with end-of-line modifications depending on your Git vintage:

$ git show :2:newfile > other_file   # extract --ours version, to new name
$ git checkout --theirs newfile
Updated 1 path from the index
$ git add newfile other_file
$ it status -s
M  newfile
A  other_file

As the (short) status shows, the two files are now ready to be committed. Their contents are:

$ cat newfile
add a file
$ cat other_file
different contents - add a file

Note that git show does not run smudge filters and does not do end-of-line conversions, at least in older versions of Git (Git has acquired the ability to do text conversions and maybe git show does them by default now—I have not tested this).

Upvotes: 1

Related Questions