Module 05: Merge Conflicts
Learning Objectives
In this module, you will:
- Understand what merge conflicts are and why they occur
- Recognize conflict markers in files
- Manually resolve merge conflicts
- Complete a merge after resolving conflicts
- Use
git statusto identify conflicted files
Challenge
Setup
Run the setup script to create your challenge environment:
.\setup.ps1
This will create a challenge/ directory with a Git repository that has conflicting changes on two branches.
Your Task
You will intentionally create and resolve a merge conflict - a crucial skill for collaborative development!
The Scenario:
- The
mainbranch andfeature-updatesbranch both modified the same lines inapp.py - When you try to merge them, Git can't automatically resolve the differences
- You must manually resolve the conflict
Steps:
- Navigate to the challenge directory:
cd challenge - Verify you're on main:
git branch - Attempt to merge feature-updates:
git merge feature-updates - Git will report a conflict! Don't panic - this is expected
- Check status:
git status(shows conflicted files) - Open
app.pyin a text editor - Look for conflict markers:
<<<<<<< HEAD (your current branch's version) ======= (the other branch's version) >>>>>>> feature-updates - Manually resolve by editing the file:
- Remove the conflict markers (
<<<<<<<,=======,>>>>>>>) - Keep the version you want, or combine both changes
- Remove the conflict markers (
- Save the file
- Mark as resolved:
git add app.py - Complete the merge:
git commit - View the result:
git log --oneline --graph
Important Notes:
- Conflicts occur when the same lines are changed differently in both branches
- Conflict markers show both versions - you choose what to keep
- You can keep one version, the other, or combine them creatively
- After editing, you MUST
git addto mark the conflict as resolved- Then
git committo complete the merge- Use
git merge --abortif you want to cancel and start over
Key Concepts
- Merge Conflict: Occurs when the same lines in a file are changed differently in both branches being merged.
- Conflict Markers: Special markers Git inserts to show the conflicting versions:
<<<<<<< HEAD: Marks the start of your current branch's changes=======: Separates the two conflicting versions>>>>>>> branch-name: Marks the end of the other branch's changes
- Conflict Resolution: The process of manually editing the file to resolve the differences and remove conflict markers.
- Staging Resolution: Using
git addto tell Git you've resolved the conflict.
Useful Commands
git merge <branch> # Attempt merge (may cause conflict)
git status # See which files have conflicts
git diff # View conflicts in detail
cat <file> # View file with conflict markers
git add <file> # Mark conflict as resolved
git commit # Complete the merge
git commit -m "message" # Complete merge with message
git merge --abort # Cancel merge and go back
Verification
Once you've resolved the conflict and completed the merge, verify your solution:
.\verify.ps1
The verification script will check that you've successfully resolved the conflict and completed the merge.
Need to Start Over?
If you want to reset the challenge and start fresh:
.\reset.ps1
This will remove the challenge directory and run the setup script again, giving you a clean slate.