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 differently in two branches.
Identifying Merge Conflicts
When a merge conflict happens, Git marks the problematic areas in the files:
<<<<<<< HEAD
Current branch changes
=======
Incoming branch changes
>>>>>>> branch-name
Conflict Resolution Workflow
1. Detect Conflicts
## Attempt to merge branches
git merge feature-branch
## Check conflict status
git status
2. Open Conflict Files
Conflicts are marked with special Git markers:
<<<<<<< HEAD
Current branch content
=======
Incoming branch content
>>>>>>> feature-branch
3. Manual Resolution Strategies
Strategy |
Description |
Keep Current |
Use current branch's changes |
Keep Incoming |
Use feature branch's changes |
Combine Manually |
Manually edit to incorporate both changes |
4. Resolve Conflicts Manually
## Edit file to remove Git markers
## Decide which changes to keep or combine
nano conflicted-file.txt
## Stage the resolved file
git add conflicted-file.txt
## Complete the merge
git commit -m "Resolve merge conflicts"
- VSCode
- GitKraken
- SourceTree
Conflict Prevention Workflow
flowchart TD
A[Pull Latest Changes] --> B[Create Feature Branch]
B --> C[Regular Commits]
C --> D[Pull Main Branch]
D --> E[Resolve Small Conflicts Early]
E --> F[Merge to Main Branch]
Advanced Conflict Resolution
Aborting Merge
## Cancel ongoing merge
git merge --abort
Resolving Complex Conflicts
- Break down large merges
- Communicate with team
- Use feature flags
LabEx recommends practicing conflict resolution in a safe environment to build confidence and skills.