Lesson 9: Resolving Conflicts
What You'll Learn
In this lesson, you'll learn how to resolve merge conflicts - one of the most important skills when working with Git. Conflicts happen when Git can't automatically merge changes, and you need to manually decide what to keep.
What is a Merge Conflict?
A merge conflict occurs when:
- Two branches modify the same lines in a file
- Git can't decide which version to keep
- You must manually resolve the conflict
Real-world analogy:
Imagine you and a friend both edit the same sentence in a shared document:
- You write: "The sky is blue"
- Friend writes: "The sky is purple"
- Conflict: Which version is correct? You need to decide!
Part 1: Creating a Conflict (for Practice)
Let's create a conflict on purpose to practice resolving it:
Step 1: Create Two Conflicting Branches
# On main branch
git switch main
echo "<h1>Welcome to My Site</h1>" > index.html
git add index.html
git commit -m "Add heading to main"
# Create and switch to feature branch
git switch -c feature/different-heading
# Make conflicting change
echo "<h1>Hello World</h1>" > index.html
git add index.html
git commit -m "Add different heading"
# Try to merge - this will create a conflict!
git switch main
git merge feature/different-heading
Result: Git can't merge automatically!
Part 2: Understanding Conflict Markers
When a conflict occurs, Git adds special markers to the file:
<<<<<<< HEAD
<h1>Welcome to My Site</h1>
=======
<h1>Hello World</h1>
>>>>>>> feature/different-heading
Understanding the Markers
| Marker | Meaning |
|---|---|
<<<<<<< HEAD |
Start of your current branch's version |
======= |
Separator between versions |
>>>>>>> branch-name |
End of the other branch's version |
Part 3: Resolving Conflicts
Step 1: Check Status
git status
You'll see:
both modified: index.html
Unmerged paths:
(use "git add <file>..." to mark resolution)
Step 2: Open the Conflicted File
Open index.html in your editor and you'll see the conflict markers.
Step 3: Decide What to Keep
You have three options:
- Keep your version (delete everything else)
- Keep their version (delete your changes)
- Keep both (combine the changes)
- Write something new (start fresh)
Step 4: Edit the File
Remove the markers and keep what you want:
<!-- Option 1: Keep first version -->
<h1>Welcome to My Site</h1>
<!-- Option 2: Keep second version -->
<h1>Hello World</h1>
<!-- Option 3: Combine both -->
<h1>Welcome to My Site - Hello World</h1>
<!-- Option 4: Write something new -->
<h1>My Awesome Website</h1>
Step 5: Stage the Resolved File
git add index.html
Step 6: Complete the Merge
git commit -m "Resolve heading conflict"
Done! The conflict is resolved and the merge is complete.
Part 4: Using VS Code to Resolve Conflicts
VS Code makes conflict resolution easier with visual tools:
- Open the conflicted file in VS Code
- You'll see buttons above the conflict:
- Accept Current Change (your version)
- Accept Incoming Change (their version)
- Accept Both Changes
- Compare Changes
- Click the option you want
- Save the file
- Stage and commit
Part 5: Aborting a Merge
If you want to cancel the merge and start over:
git merge --abort
This returns everything to how it was before the merge attempt.
Part 6: Preventing Conflicts
Best Practices
| Practice | How It Helps |
|---|---|
| Pull frequently | Stay up-to-date with team changes |
| Make small, focused commits | Easier to identify and fix conflicts |
| Communicate with team | Know who's working on what files |
| Merge main into feature branches regularly | Catch conflicts early |
| Work on different files when possible | Reduces chance of conflicts |
Part 7: Multiple File Conflicts
If multiple files have conflicts:
# Check which files have conflicts
git status
# Resolve each file one by one
# 1. Edit the file
# 2. Remove conflict markers
# 3. Stage the file
git add file1.html
git add file2.css
# When all files are resolved, commit
git commit -m "Resolve merge conflicts"
Practice Exercise
Practice resolving a conflict:
- Create a file with some content
- Commit it to main
- Create a branch and modify the same line
- Switch back to main and modify that line differently
- Try to merge - you'll get a conflict!
- Resolve the conflict
- Complete the merge
# Create initial file
echo "Hello" > greeting.txt
git add greeting.txt
git commit -m "Add greeting"
# Create branch and change it
git switch -c branch-a
echo "Hello World" > greeting.txt
git commit -am "Change to Hello World"
# Change it differently on main
git switch main
echo "Hello There" > greeting.txt
git commit -am "Change to Hello There"
# Create conflict
git merge branch-a
# Now resolve it! Edit greeting.txt
# Remove markers, choose what to keep
# Then:
git add greeting.txt
git commit -m "Resolve greeting conflict"
Summary
Conflict Resolution Steps
| Step | Command/Action |
|---|---|
| 1. Identify conflict | git status |
| 2. Open file | Look for conflict markers |
| 3. Edit file | Remove markers, keep what you want |
| 4. Stage file | git add filename |
| 5. Complete merge | git commit |
Remember: Conflicts are normal! Don't panic - just take it step by step.
What's Next?
In the next lesson, you'll learn about Pull Requests - GitHub's way of reviewing code before merging. This is how teams collaborate on professional projects!