Merge Conflict Overview
Understanding Merge Conflicts
Merge conflicts occur when Git cannot automatically resolve differences between two commits. This typically happens when:
- The same part of a file is modified in different ways in two branches
- Changes are made to the same line or nearby lines of code
Conflict Visualization
graph TD
A[Branch A] -->|Modify Same Line| C{Merge Conflict}
B[Branch B] -->|Modify Same Line| C
C -->|Manual Resolution| D[Merged Code]
Common Conflict Scenarios
Scenario |
Description |
Line Modification |
Different changes to the same line |
File Deletion |
One branch deletes, another modifies |
File Renaming |
Conflicts in file structure |
Conflict Markers
When a conflict occurs, Git marks the file with special markers:
<<<<<<< HEAD
Current branch code
=======
Incoming branch code
>>>>>>> branch-name
Conflict Detection
## Attempt to merge branches
git merge feature-branch
## If conflict occurs
## Conflict files will be marked with conflict markers
Resolution Strategies
- Manual Editing
- Using merge tools
- Choosing specific changes
Practical Example
## Create conflicting changes
echo "Original content" > file.txt
git add file.txt
git commit -m "Initial commit"
## Create two branches with different modifications
git checkout -b branch-a
echo "Branch A modification" > file.txt
git commit -am "Branch A change"
git checkout main
git checkout -b branch-b
echo "Branch B modification" > file.txt
git commit -am "Branch B change"
## Attempt merge (will cause conflict)
git merge branch-a
Best Practices
- Communicate with team members
- Pull changes frequently
- Use clear commit messages
LabEx recommends developing a systematic approach to handling merge conflicts efficiently.