Resolving Merge Conflicts
What are Merge Conflicts?
Merge conflicts occur when Git cannot automatically resolve differences in code between two commits. This typically happens when the same part of a file has been modified in different ways across different branches.
Merge Conflict Workflow
graph TD
A[Attempt Merge] --> B{Conflict Detected?}
B -->|Yes| C[Identify Conflict]
B -->|No| D[Merge Successful]
C --> E[Manual Resolution]
E --> F[Mark as Resolved]
F --> G[Complete Merge]
Identifying Merge Conflicts
When a merge conflict occurs, Git marks the problematic areas in the file:
<<<<<<< HEAD
Current branch code
=======
Incoming branch code
>>>>>>> branch-name
Resolving Conflicts Step by Step
## Attempt to merge branches
git merge feature-branch
## If conflicts occur, view conflicting files
git status
## Open conflicting files in a text editor
nano conflicting_file.txt
## Manually edit the file to resolve conflicts
## Remove conflict markers
## Choose the correct code or combine changes
Conflict Resolution Strategies
Strategy |
Description |
Use Case |
Keep Current |
Use code from current branch |
Minor differences |
Keep Incoming |
Use code from incoming branch |
Preferred new implementation |
Manually Merge |
Combine code from both branches |
Complex changes |
Marking Conflict as Resolved
## After manually resolving conflicts
git add conflicting_file.txt
## Complete the merge
git commit -m "Resolved merge conflicts"
Advanced Conflict Resolution
## Use merge tool for visual conflict resolution
git mergetool
## Abort merge if too complex
git merge --abort
Common Conflict Scenarios
- Editing the same line in different branches
- Deleting a file in one branch while modifying in another
- Renaming files with conflicting changes
LabEx Tip
At LabEx, we recommend communicating with team members before merging branches to minimize potential conflicts and ensure smooth collaboration.