Handling Merge Conflicts¶
Merge conflicts happen when Git can't automatically combine changes from different sources. While they might seem scary at first, they're a normal part of working with Git, especially when collaborating with others.
What Causes Merge Conflicts?¶
Conflicts occur when:
- Two people edit the same line in a file
- One person edits a file while another deletes it
- Two branches modify the same part of a file differently
Understanding Conflict Markers¶
When Git encounters a conflict, it marks the conflicting sections in your files:
<<<<<<< HEAD
This is the content from your current branch
=======
This is the content from the branch you're merging
>>>>>>> other-branch
<<<<<<< HEAD
- Start of your changes=======
- Separator between conflicting changes>>>>>>> other-branch
- End of incoming changes
Resolving Conflicts Step-by-Step¶
Step 1: Identify Conflicted Files¶
Look for files marked as "both modified" or with conflict indicators.
Step 2: Open and Edit Conflicted Files¶
- Open the file in your text editor
- Find the conflict markers (
<<<<<<<
,=======
,>>>>>>>
) - Decide which changes to keep:
- Keep your changes
- Keep their changes
- Keep both changes
- Write something completely new
Step 3: Remove Conflict Markers¶
After deciding what to keep, remove ALL conflict markers:
Step 4: Stage the Resolved Files¶
Step 5: Complete the Merge¶
Hands-on Practice¶
Let's create and resolve a merge conflict:
Create a Practice Repository¶
mkdir conflict-practice
cd conflict-practice
git init
# Create a file with initial content
echo "Welcome to our project" > README.md
echo "This is the main documentation" >> README.md
git add README.md
git commit -m "Initial README"
Create Conflicting Changes¶
# Create and switch to a new branch
git checkout -b feature-branch
# Modify the file on the feature branch
echo "Welcome to our amazing project" > README.md
echo "This is the main documentation" >> README.md
echo "Added new feature documentation" >> README.md
git add README.md
git commit -m "Update README with feature info"
# Switch back to main branch
git checkout main
# Make different changes to the same line
echo "Welcome to our wonderful project" > README.md
echo "This is the main documentation" >> README.md
echo "Updated main branch documentation" >> README.md
git add README.md
git commit -m "Update README from main branch"
Create the Conflict¶
Git will report a conflict! Now let's resolve it.
Resolve the Conflict¶
<<<<<<< HEAD
Welcome to our wonderful project
This is the main documentation
Updated main branch documentation
=======
Welcome to our amazing project
This is the main documentation
Added new feature documentation
>>>>>>> feature-branch
Edit the file to resolve the conflict. For example:
Welcome to our amazing and wonderful project
This is the main documentation
Updated main branch documentation
Added new feature documentation
Then finish the merge:
# Stage the resolved file
git add README.md
# Complete the merge
git commit -m "Resolve README conflict by combining descriptions"
Conflict Resolution Strategies¶
Strategy 1: Accept One Side Completely¶
# Keep your changes (current branch)
git checkout --ours filename.txt
# Keep their changes (incoming branch)
git checkout --theirs filename.txt
# Stage the file
git add filename.txt
Strategy 2: Manual Resolution¶
Edit the file to combine changes thoughtfully, considering:
- What makes the most sense functionally?
- Which changes are more recent or important?
- How can you combine both sets of changes?
Strategy 3: Use a Merge Tool¶
Configure a visual merge tool:
# Configure VS Code as merge tool
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'
# Use the merge tool
git mergetool
Preventing Conflicts¶
Best Practices¶
- Communicate with your team - Coordinate who's working on what
- Make smaller, focused commits - Easier to resolve conflicts
- Pull frequently - Stay up-to-date with changes
- Work on different files when possible - Natural conflict avoidance
Pull Before You Push¶
# Before starting work
git pull origin main
# Before pushing your changes
git pull origin main
git push origin your-branch
When Things Go Wrong¶
Abort a Merge¶
If you want to cancel a merge in progress:
Reset After a Bad Merge¶
If you completed a merge but want to undo it:
# Find the commit before the merge
git log --oneline
# Reset to that commit (replace abc123 with actual hash)
git reset --hard abc123
Use Reset Carefully
git reset --hard
permanently discards changes. Make sure you really want to lose the merge.
Common Conflict Scenarios¶
Scenario 1: Different Messages¶
<<<<<<< HEAD
console.log("Hello from main branch");
=======
console.log("Hello from feature branch");
>>>>>>> feature-branch
Resolution: Choose the most appropriate message or combine them.
Scenario 2: Added vs Modified¶
<<<<<<< HEAD
function oldFunction() {
return "original";
}
=======
function newFunction() {
return "updated";
}
function anotherFunction() {
return "additional";
}
>>>>>>> feature-branch
Resolution: Decide whether to keep old functionality, new functionality, or both.
Essential Commands Summary¶
Command | Purpose |
---|---|
git status |
Check for conflicts |
git merge branch-name |
Merge a branch (may cause conflicts) |
git add file |
Stage resolved file |
git commit |
Complete the merge |
git merge --abort |
Cancel merge in progress |
git mergetool |
Open visual merge tool |
What's Next?¶
Congratulations! You now have a solid foundation in Git. These skills will serve you well as you collaborate on projects and manage your code.
For more advanced Git topics, check out our Git Quick Reference or explore these resources:
- Pro Git Book - Complete Git documentation
- GitHub Guides - Collaboration workflows
- Atlassian Git Tutorials - Advanced concepts
Practice Exercises¶
- Create intentional conflicts - Practice with different types of conflicts
- Try different resolution strategies - Use
--ours
,--theirs
, and manual resolution - Set up a merge tool - Configure VS Code or another visual tool for resolving conflicts