What is merge conflict and how does it occur?

Conflict merge occurs when changes have been made to the same part of a file in both the source (branch A) and target (branch B) branches, and the system cannot determine which version to keep, consider the following example:'

Sara creates a new Git repository and initializes it with a file called example.txt.

# example.txt
This is the initial content.

Sara’s Changes:

  • Sara makes changes to example.txt on her local branch, feature/sara-branch.
# example.txt (feature/sara-branch)
This is Sara's modification.

Sara commits her changes.

Meanwhile, John also makes changes to example.txt on his local branch, feature/john-branch.

# example.txt (feature/john-branch) 
John's update is here.

John commits his changes. and get it merged to main

  • Now, both Sara and John want to merge their feature branches into the main branch.

  • When Sara tries to merge her changes, a conflict occurs because John has modified the same lines in example.txt

# example.txt (conflict markers)
<<<<<<< HEAD
This is Sara's modification.
=======
John's update is here.
>>>>>>> feature/john-branch
updatedupdated2024-01-172024-01-17