Resolving Conflicts
Understanding Git Conflicts
Conflicts occur when Git cannot automatically merge changes from different branches due to overlapping modifications.
Conflict Markers
flowchart TD
A[Original Code] --> B{Conflict Detected}
B --> |Conflict Markers| C[<<<<<<< HEAD]
B --> |Current Branch Changes| D[Your Changes]
B --> |Incoming Branch Changes| E[Incoming Changes]
C --> F[=======]
F --> D
D --> G[>>>>>>> branch-name]
Conflict Resolution Strategies
Strategy |
Description |
Use Case |
Manual Editing |
Directly modify conflicting files |
Small, manageable conflicts |
Visual Merge Tools |
Use GUI tools for conflict resolution |
Complex conflicts |
Choosing Versions |
Select entire current or incoming changes |
Simple binary decisions |
Detailed Conflict Resolution Process
1. Identify Conflicting Files
## Check conflict status
git status
## Show detailed conflict information
git diff
2. Open Conflicting File
## Example using nano editor
nano conflicting_file.py
3. Resolve Conflict Manually
## Conflict marker example
<<<<<<< HEAD
current_branch_code()
=======
incoming_branch_code()
>>>>>>> branch-name
4. Edit and Remove Markers
## Correct resolution
def resolved_function():
## Combine or choose appropriate implementation
pass
5. Stage and Complete Resolution
## Stage resolved file
git add conflicting_file.py
## Continue rebase
git rebase --continue
Advanced Conflict Resolution Techniques
## Configure merge tool
git config --global merge.tool vscode
## Invoke merge tool
git mergetool
Conflict Resolution Options
## Keep current branch changes
git checkout --ours file
## Keep incoming branch changes
git checkout --theirs file
Common Conflict Scenarios
- Modifying same line in different branches
- Renaming or moving files
- Adding/removing same code block
Best Practices
- Communicate with team members
- Pull changes frequently
- Use feature branches
- Write clear, modular code
LabEx Recommendation
Practice conflict resolution in a safe environment to build confidence and skill.
Potential Pitfalls
- Accidentally introducing bugs
- Incomplete conflict resolution
- Overlooking subtle code changes