ChatGPT解决这个技术问题 Extra ChatGPT

What exactly is a merge conflict?

I have made a git repository and added a text file to it. This is 100% for learning purpose.

I added "1" to the text file and committed it to master. Created a new branch from master and appended "2". Finally, created a branch from master and appended "3".

Could you please explain how a conflict may occur in this, or any other, scenario?


V
VonC

You will have a conflict if you merge:

branch2 to master (no conflict)

branch3 to master (conflict):

That is because:

The common ancestor would be master (with a second line empty)

the source content is branch3 (with a second line including "3")

the destination content is on latest of master (with a second line including "2", from the merge of branch2 to master)

Git will ask you to choose which content to keep ("3", "2", or both).

First, do the merges after:

git config merge.conflictstyle diff3

See "Fix merge conflicts in Git?".

Notes:

With Git 2.30 (Q1 2021), a new default merge strategy is in place: ORT ("Ostensibly Recursive's Twin"), with clearer conflict messages (Gti 2.36, Q2 2022)

you can preview those conflicts with (Git 2.38, Q3 2022): git merge-tree --write-tree --no-messages branch1 branch2 (That would not touch the index or working tree!)


I tried and got it. Thank you. So is it simply like if I add some new things it adds without any confusion/conflict, but if I am going to replace something it will cause confusion to replace it or keep original?
@user3693167 yes, because the same lines are modified twice, which triggers a manual resolution of the merge (conflict).
@user3693167 yes, in term of overlapping blocks of content (help.github.com/articles/…) See also gitguys.com/topics/…
@Dumoko Yes, it is a three-way merge (stackoverflow.com/a/4130766/6309), also shown in stackoverflow.com/a/31227165/6309
@Vin this is not about order, but about concurrent modifications done since the last common ancestor. Hence conflict.
C
Caleb

A merge conflict happens when two branches both modify the same region of a file and are subsequently merged. Git can't know which of the changes to keep, and thus needs human intervention to resolve the conflict.

In this case, your steps 2 and 3 create two branches that have conflicting changes.


What do you mean by "region"? How big are those?
@GuillaumeF. For text files, conflicts happen when two commits change the same line(s) in a file. I'm not sure how it works for binary files (e.g. images, sounds, etc.).
@GuillaumeF. infinitely big
f
faisal_kk

I understand this is an old question but in case you would like to know in an intuitive way the algorithms used by Git to compare two files.It will clarify the doubts on how overlapping regions works with diff.

Here is an explanation of one of the popular algorithms which was developed by Eugene W. Myers. In this approach finding the shortest edit script (SES) is modelled as a graph search. Here is really good article by James Coglan on the same with an example - The Myers diff algorithm