Resolving Merge Conflicts
Merge conflicts occur when Git can't automatically combine changes from different branches. This happens when the same lines of code are modified differently in both branches.
When Do Conflicts Happen?
Conflicts occur when:
- The same lines are changed in both branches
- A file is modified in one branch and deleted in another
- The same file is added with different content in both branches
Example Scenario
main: Changed line 5 to "Hello World"
feature: Changed line 5 to "Hello Git"
Git: "I don't know which one you want!"
Anatomy of a Conflict
When a conflict occurs, Git marks the conflicting sections:
function greet() {
<<<<<<< HEAD
return "Hello World";
=======
return "Hello Git";
>>>>>>> feature
}
| Marker | Meaning |
|---|---|
<<<<<<< HEAD | Start of your current branch's version |
======= | Separator between versions |
>>>>>>> feature | End, shows the incoming branch name |
Everything between <<<<<<< and ======= is YOUR version.
Everything between ======= and >>>>>>> is THEIR version.
The Conflict Resolution Process
Step 1: Attempt the Merge
git merge feature
If there are conflicts:
Auto-merging file.js
CONFLICT (content): Merge conflict in file.js
Automatic merge failed; fix conflicts and then commit the result.
Step 2: Check Status
git status
On branch main
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: file.js
Step 3: Open the Conflicted File
View and edit the file to resolve conflicts:
// Before resolution:
function greet() {
<<<<<<< HEAD
return "Hello World";
=======
return "Hello Git";
>>>>>>> feature
}
// After resolution (you choose what to keep):
function greet() {
return "Hello Git World"; // Combined both!
}
Step 4: Mark as Resolved
git add file.js
Step 5: Complete the Merge
git commit
# Git auto-generates a merge commit message
Or with a custom message:
git commit -m "Merge feature: resolve greeting conflict"
Resolution Strategies
Keep Ours
Keep your version, discard theirs:
// Remove their version and conflict markers
function greet() {
return "Hello World";
}
Keep Theirs
Keep their version, discard yours:
function greet() {
return "Hello Git";
}
Combine Both
Merge the logic from both:
function greet() {
return "Hello Git World";
}
Rewrite Completely
Sometimes neither version is right:
function greet(name) {
return `Hello ${name}`;
}
Using Merge Tools
Built-in Merge Tool
git mergetool
Git will open your configured merge tool.
Configure a Merge Tool
# Use VS Code
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'
# Use vimdiff
git config --global merge.tool vimdiff
VS Code Conflict Resolution
VS Code shows conflict markers with helpful buttons:
- "Accept Current Change" (yours)
- "Accept Incoming Change" (theirs)
- "Accept Both Changes"
- "Compare Changes"
Aborting a Merge
If you want to give up on the merge:
git merge --abort
This returns your branch to the state before the merge started.
Preventing Conflicts
1. Communicate with Your Team
Coordinate who's working on what files.
2. Pull Frequently
git pull origin main
Stay up-to-date with changes.
3. Make Smaller, Focused Changes
Large changes touching many files increase conflict probability.
4. Merge Main into Your Branch Regularly
git switch feature
git merge main # Handle conflicts while they're small
Exercise: Resolve a Conflict
Practice resolving a merge conflict:
Complex Conflict Scenarios
File Deleted in One Branch
CONFLICT (modify/delete): file.js deleted in feature and modified in HEAD.
Options:
# Keep the file
git add file.js
# Delete the file
git rm file.js
File Added in Both Branches
CONFLICT (add/add): Merge conflict in config.js
Both branches added the same file with different content. Resolve like a normal content conflict.
Binary File Conflicts
Binary files (images, etc.) can't be merged textually:
# Keep ours
git checkout --ours image.png
git add image.png
# Keep theirs
git checkout --theirs image.png
git add image.png
Using git checkout During Conflicts
During a conflict, you can choose versions:
# Use our version entirely
git checkout --ours file.js
# Use their version entirely
git checkout --theirs file.js
# Then mark as resolved
git add file.js
Checking Conflict Status
# See conflicted files
git diff --name-only --diff-filter=U
# See conflict details
git diff
# See specific file
git diff file.js
Summary
- Conflicts happen when Git can't auto-merge changes
- Conflict markers show both versions in the file
- Resolution: edit file →
git add→git commit - Use
git merge --abortto cancel - Use
--oursor--theirsfor quick resolution - Merge tools make resolution easier
- Prevent conflicts by pulling often and communicating
In the next module, we'll learn about remote repositories and collaborating with others.

