complete , The correct answer has multiple parts . first , We must first conduct a normal three-way merger ( stay Git Required in -s Recursive or -s
resolve, If used -s recursion , Then find a single merge library and two other submissions ). however , You may want to skip to the third part .

Elements required for normal three-way merging

Do you want to make any consolidation , You need :

> Consolidation basis submission B,1

> Current submission L( left , also called HEAD), among B For ancestors ,

> Another submission R, Also B As an ancestor .

because “is ancestor” Allow the nodes in the submitted graph to be equal ( Technically, it is previous or equivalent ≼ compare ), So there may be B = L and / or B =
R. however , If this is the case , Then no consolidation is required , If you force a merger ( use git merge –no-ff- That means B =
L and L≺R; Two non mandatory cases are “ Fast forward ” Instead of actual consolidation and “ No consolidation “ error ” There will be no merge conflicts . Therefore, we can assume that the consolidation basis is before both sides of the consolidation .

1 use –allow-unrelated-histories, If L and R There is no lowest common ancestor node , Can be used in an empty tree Git Replace actual basic submission . however , This will cause all identified files to be added / Add conflict .

Given merge , Elements required for conflicts

next , To conflict in a file path , You need what I call “ High level ” Conflict or “ Low level ” conflict ( Or both ). to this end ,Git Must identify ( Namely match )B,L and R Files in . Due to the ability to add new files , Renaming and deleting files , This does not require the file to have the same name in all three submissions . especially :

> If path P Exist in B,L and R Of all three , Has a path P The three files are identified together . ( in other words , There is a path PB, such as path / to /
foo.txt, It has a matching PL route / reach / foo.txt and PR route / reach /
foo.txt. Obviously, this file is “ Same file ” Throughout the consolidation period , therefore Git Identify the three paths as one file .)

> perhaps , There can be up to three different paths ,PB path / to / basename,PL path2 / to2 / left, as well as PR path3 /
to3 / right. One or more of these may not even exist . This leads to : add to / Add conflict , If ∄PB and PL =
PR; rename / Delete conflict , If PB Equal to one of the left or right paths , But the other path does not exist ; Or rename / Rename conflict , If PB≠PL≠PR.

If there is no high-level conflict ( add to / add to , rename / Rename or rename / delete ), The rename may still exist / Modify or rename / Delete conflict , as long as blob Hash of ID( File content )
) The names given by two or three path names do not match .

All these “ High level ” Conflicts can lead to merge conflicts , It does not result in any conflicting tags . to this end , We must now actually merge the base file with the two side files ( This means that all three files must exist , Or for adding / Add case , We use a simple empty file as the base version ).

This merge may or may not have its own conflicts . If the merge adds low-level conflicts , We will get the conflict flag . If there is no high-level conflict ( The paths in all three submissions are the same ) But it needs to be completely merged ( Hash values are different ), We may have low-level conflicts with conflict markers .

Git What is needed to resolve conflicts in files

To get merge conflicts in a file in the work tree ,Git You must see that the left and right versions have changed the same line , However, changes were made in different ways .
( please remember , All three hash values must be different , therefore Git Combining a set of changes , It's actually a difference PB PL And difference PB PR Change of .)

Most obvious , The least confusing case

The most obvious thing happened on one side ( Let's choose the left first ) say :

unchanged context

-changed line

+replacement 1

more unchanged context

Another said :

unchanged context

-changed line

+replacement 2

more unchanged context

Delete one or more lines here , Instead, insert one or more rows , But the inserted rows do not match . under these circumstances , Merge conflicting styles to represent them as :

unchanged context

<<<<<<< left-label

replacement 1

=======

replacement 2

>>>>>>> right-label

more unchanged context

diff3 The context style renders this as :

unchanged context

<<<<<<< left-label

replacement 1

||||||| merged common ancestors

changed line

=======

replacement 2

>>>>>>> right-label

more unchanged context

If we just add text , But add different text on the same line , We will also encounter merger conflicts . same , The added text from each side is displayed in the conflict marker . ( If we add the same text to both sides –
As new text , Or as replacement text for the change line ,Git A copy of this addition will be obtained , And there is no conflict .)

Some confusing cases

If one but not both substitution lines are empty – Namely , If left or right diff read :

unchanged context

-changed line

more unchanged context

Then missing merge or diff3 One but not two alternate lines in the style tag file . ( If two diff Delete only original rows , There is no conflict :Git Delete only one .)

same , If one party adds a row above or below the row deleted by the other party , There will be a conflict . This conflict indicates reservations – Then add a There are wires on one side of the , There is no line on the other side . for example :

some merge conflict.

Line that will conflict.

+add line below it

Rest of the

VS:

some merge conflict.

-Line that will conflict.

Rest of the

( If you add the upper row instead of the lower row , The same will happen ).

This is diff3 Conflict style is very useful . The following is the entire merged file for one of these cases :

We need a base file

in which to make

some merge conflict.

<<<<<<< HEAD

||||||| merged common ancestors

Line that will conflict.

=======

Change the line that will conflict.

>>>>>>> b2

Rest of the

base file for the

merge conflict example.

Please note that , It's obvious now – Or at least not so mysterious – One line will be read, and the line will conflict . In the basic version , I'm completely from the left HEAD Version deletion , And replace with the right b2 Different lines in version .

Technology