Dziamid
Dziamid

Reputation: 11581

Incorporating both changes when resolving git merge confilct

Let's say we have 2 development branches (master and feature) and both branches added some code to the same file. When trying to merge feature into master we run into a single confict:

++<<<<<<< HEAD
+ //some code added in master branch
++=======
+ //some code added in feature branch
++>>>>>>> feature

If I want to accept only HEAD (master) and abandon feature I would run:

git checkout --ours path/to/file

If I want to accept only feature (master) and abandon HEAD I would run:

git checkout --theirs path/to/file

How can I accept both changes so that the result of the conflict resolution would be as simple union of the code?

//some code added in master branch
//some code added in feature branch

Upvotes: 0

Views: 45

Answers (2)

Dziamid
Dziamid

Reputation: 11581

As Nevik pointed, a simple removal of markers would result in the union of the code. Here's my bash script that does the job:

function git.both() {
  filename=$1
  tempfilename="temp_file_used_by_git_both"
  grep -v "<<<<<<<\|>>>>>>>\|=======" $filename > $tempfilename
  mv $tempfilename $filename

}

Usage:

git.both path/to/file

Upvotes: 0

Nevik Rehnel
Nevik Rehnel

Reputation: 51945

You will have to edit the file manually, and remove the conflict markers (if you only do that, the result is exactly the "union" you want).

Git will not do this because conflict resolution is a semantic problem to which a program cannot offer a general solution.

Though if you do this often or on a large scale, you could doubtless write a script for it (doing this in an automated way is likely to break your code though).

Upvotes: 1

Related Questions