Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is a merge conflict?

Tags:

git

git-merge

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?

like image 275
user3693167 Avatar asked Jul 20 '14 15:07

user3693167


People also ask

What is a merge conflict?

A merge conflict is an event that occurs when Git is unable to automatically resolve differences in code between two commits. When all the changes in the code occur on different lines or in different files, Git will successfully merge commits without your help.

What causes merge conflicts in Git?

Git can handle most merges on its own with automatic merging features. A conflict arises when two separate branches have made edits to the same line in a file, or when a file has been deleted in one branch but edited in the other. Conflicts will most likely happen when working in a team environment.

How do you identify a merge conflict?

To see the beginning of the merge conflict in your file, search the file for the conflict marker <<<<<<< . When you open the file in your text editor, you'll see the changes from the HEAD or base branch after the line <<<<<<< HEAD .


1 Answers

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?".

like image 178
VonC Avatar answered Sep 21 '22 06:09

VonC