59

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

  1. I added "1" to the text file and committed it to master.

  2. Created a new branch from master and appended "2".

  3. Finally, created a branch from master and appended "3".

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

Flip
  • 6,233
  • 7
  • 46
  • 75
user3693167
  • 783
  • 1
  • 9
  • 16

3 Answers3

45

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:

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 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 Jul 20 '14 at 16:43
  • 1
    @user3693167 yes, because the same lines are modified twice, which triggers a manual resolution of the merge (conflict). – VonC Jul 20 '14 at 16:44
  • Thank you. Is it tracking it with line numbers ? – user3693167 Jul 20 '14 at 16:46
  • 1
    @user3693167 yes, in term of overlapping blocks of content (https://help.github.com/articles/resolving-a-merge-conflict-from-the-command-line) See also http://www.gitguys.com/topics/merging-with-a-conflict-conflicts-and-resolutions/ – VonC Jul 20 '14 at 16:49
  • So the biggest thing about this answer is the following: git decides on where the conflict are taking into consideration the common ancestor. – Dumoko Oct 23 '15 at 04:51
  • 1
    @Dumoko Yes, it is a three-way merge (http://stackoverflow.com/a/4130766/6309), also shown in http://stackoverflow.com/a/31227165/6309 – VonC Oct 23 '15 at 06:03
  • @VonC I get that they both changed the same code, but doesn't merge order defines which changes are applied? In this case changes by branch2 are overwritten by branch3.. Why is a conflict thrown? Is it because it is changed, and no new code is added? Or what exactly caused the conflict? – Vincent Jul 29 '16 at 09:08
  • 1
    @Vin this is not about order, but about concurrent modifications done since the last common ancestor. Hence conflict. – VonC Jul 29 '16 at 09:48
  • @VonC, when you try to merge `branch2` to master, no conflicts will arise as you said. But why ? Both `master` and `branch2` changed the same file ? How does git decide what content to keep in merge - 1,2 or both ? – Istiaque Ahmed Jun 08 '23 at 07:47
  • @IstiaqueAhmed There is conflict only if common lines were modified concurrently. See "[Why is a 3-way merge advantageous over a 2-way merge?](https://stackoverflow.com/a/4130766/6309)" and "[While merging, does Git only consider the timestamp of a commit rather than which branch it belongs to?](https://stackoverflow.com/a/31227165/6309)". – VonC Jun 08 '23 at 08:07
41

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.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 2
    What do you mean by "region"? How big are those? – Guillaume F. Jan 04 '20 at 21:33
  • @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.). – Caleb Jan 06 '20 at 12:54
  • @GuillaumeF. infinitely big – umer Feb 15 '20 at 03:33
  • @Caleb, can you explain what you mean by 'are subsequently merged' ? – Istiaque Ahmed Jun 08 '23 at 07:49
  • 1
    @IstiaqueAhmed Let's say you've got a shared repo with some common branch C, and you and a friend each create your own branches. We'll call your branch D and your friend's branch E. You each proceed to modify the same part of the same file. Now when you try to merge D and E back into C, you'll find that the second merge will result in a conflict. – Caleb Jun 08 '23 at 17:51
3

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

faisal_kk
  • 1,039
  • 1
  • 11
  • 19