Can git merge two files ignoring their common ancestor

For example, I have the following file:

// first part function() { var timer = 1; } // second part function() { var timer = 2; } // third part function() { var timer = 3; } 

Now two different branches change it. I have a change:
in branch 1:

 // first part function() { var timer = "changed in branch1"; } // second part function() { var timer = 2; } // third part function() { var timer = 3; } 

in branch2:

 // first part function() { var timer = 1; } // second part function() { var timer = "changed in branch2"; } // third part function() { var timer = 3; } 

With tripartite merging, these changes will merge into purity because each branch has changed a piece of code that is different from the other. However, when combining, I want the following:

 // first part function() { <<<<<<< branch1 var timer = "changed in branch1"; ======= var timer = 1; >>>>>>> branch2 } // second part function() { <<<<<<< branch1 var timer = 2; ======= var timer = "changed in branch2"; >>>>>>> branch2 } // third part function() { var timer = 3; } 

Is there any way to achieve such a merge in git? I believe that based on the comments, this kind of merge would be something like this:

  • make diff file
  • paste the pieces into the final version not in diff
  • mark fragments that differ in conflict markers.
+6
source share
3 answers

Git offers the plumbing git merge-file command, which allows you to merge two files without a database, however there is no easy way to invoke this. If you combine branch into master and want to unreasonably merge some foo.txt file:

 git show master:foo.txt > /tmp/master.foo.txt git show branch:foo.txt > /tmp/branch.foo.txt echo -n > /tmp/empty.txt git merge-file -p -L master -L base -L branch /tmp/master.foo.txt /tmp/empty.txt /tmp/branch.foo.txt > foo.txt 

In the above example, this gives:

 // first part function() { <<<<<<< master var timer = 1; ======= var timer = "changed in branch1"; >>>>>>> branch } // second part function() { <<<<<<< master var timer = "changed in branch2"; ======= var timer = 2; >>>>>>> branch } // third part function() { var timer = 3; } 

(Scripting is left as an exercise for the reader.)

+1
source

If you just want to see the differences between your code and a specific commit, just do:

 git diff HEAD..<sha-commit2> 

or if you want to compare the current commit with the end of another branch:

 git diff HEAD..other_branch 

EDIT As @Maximus asked in a comment: if you want to see changes made by someone

  • on the main branch
  • in certain files
  • from the moment of branching

So, first you need to find a common ancestor: (I suppose you are on your branch, from where I use HEAD. May be replaced by your branch name)

 git merge-base HEAD master 

then you can see the changes, since this common ancestor in certain files ( git log -p or git diff ; depends on the result you need):

 git log -p <sha-common-ancestor>..master -- <the files and dirs you want> git diff <sha-common-ancestor>..master -- <the files and dirs you want> 
+1
source

Use git merge --no-commit , then correct it after the fact before executing the merge result. This is the easiest method (certainly) for one-time use.

If you need a repeatable method for automatically executing it, you can define a custom merge driver (see, for example, this nabble thread ). However, you should come up with some kind of systematic rules on how to integrate apparently non-conflict changes.

+1
source

Source: https://habr.com/ru/post/984304/


All Articles