Resolving Git Conflicts
Understanding Git Conflicts
What is a Merge Conflict?
graph TD
A[Branch A] --> C[Conflicting Commit]
B[Branch B] --> C
C --> D{Conflict Resolution}
Conflict Types
Conflict Type |
Description |
Line-level Conflicts |
Different changes on the same line |
File-level Conflicts |
Modifications in the same file |
Structural Conflicts |
Significant code structure changes |
Detecting Conflicts
## Check repository status
git status
## Show conflict details
git diff
Conflict Resolution Strategies
Manual Resolution
## Open conflicting file
nano conflicted_file.txt
## Manually edit conflict markers
<<<<<<< HEAD
Current branch changes
=======
Incoming branch changes
>>>>>>> branch-name
Conflict Marker Explanation
<<<<<<< HEAD ## Start of current branch changes
Current changes
======= ## Separator
Incoming changes
>>>>>>> branch-name ## End of incoming branch changes
Resolving Conflicts
Option 1: Keep Current Changes
## Choose current branch changes
git checkout --ours filename
Option 2: Keep Incoming Changes
## Choose incoming branch changes
git checkout --theirs filename
Option 3: Merge Manually
## Stage resolved file
git add filename
## Complete merge
git commit
Advanced Conflict Resolution
graph TD
A[Detect Conflict] --> B{Resolution Strategy}
B --> |Manual| C[Edit File]
B --> |Automatic| D[Use Git Tools]
C --> E[Stage Changes]
D --> E
E --> F[Commit Resolution]
Best Practices
- Communicate with team members
- Pull changes frequently
- Use feature branches
- Review conflicts carefully
LabEx Tip
LabEx recommends practicing conflict resolution in controlled environments to build practical skills.
Conflict Prevention Techniques
## Rebase instead of merge
git pull --rebase origin main
## Use merge tools
git mergetool