Git Merge Conflicts — How to Resolve Them
In this tutorial, you'll learn about Git Merge Conflicts. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
What You'll Learn
Understand and resolve Git merge conflicts — what they are, how to resolve them in VS Code and CLI, and how to reduce conflicts on your team.
Why It Matters
Merge conflicts are inevitable in collaborative development. Knowing how to resolve them quickly keeps you productive and prevents bad merges.
Real-World Use
Two developers modified the same function, merging a long-lived branch into main, or rebasing a feature branch that conflicts with recent changes.
What Causes Conflicts?
A conflict happens when two branches modify the same part of the same file in different ways:
Branch A: echo "Hello" > greeting.txt # "Hello"
Branch B: echo "Hi" > greeting.txt # "Hi"
git merge: CONFLICT! Which one wins?
Conflict Markers
Git marks conflicts with special markers in the file:
<<<<<<< HEAD
This is the current branch's version
=======
This is the incoming branch's version
>>>>>>> feature/other-branch
| Marker | Meaning |
|---|---|
<<<<<<< HEAD |
Start of the current branch's changes |
======= |
Separator between both versions |
>>>>>>> feature |
End of the incoming branch's changes |
Resolving in Command Line
# 1. Try to merge
git merge feature/login
# CONFLICT in login.py
# 2. See conflicted files
git status
# both modified: login.py
# 3. Open the file and fix it
vim login.py
# Edit: remove markers, keep the correct code
# 4. Mark as resolved
git add login.py
# 5. Continue
git commit
# Or for rebase:
git rebase --continue
Resolving in VS Code
VS Code has built-in merge Conflict Resolution:
<<<<<<< HEAD ← Current Change
console.log("old");
======= ← Incoming Change
console.log("new");
>>>>>>> feature/login ← Incoming Change
VS Code shows:
- Accept Current — Keep only the HEAD version
- Accept Incoming — Keep only the other branch's version
- Accept Both — Keep both (in order)
- Compare Changes — Side-by-side diff
Click one button and the markers are gone.
Resolving in IntelliJ/WebStorm
IntelliJ offers a three-pane merge tool:
Left (Yours) | Center (Result) | Right (Theirs)
You manually resolve each conflict or click "Accept Left" / "Accept Right".
Aborting a Merge
# If things go wrong
git merge --abort
# For rebase
git rebase --abort
# For cherry-pick
git cherry-pick --abort
Using git mergetool
# Configure a merge tool
git config merge.tool vimdiff
# Or:
git config merge.tool vscode
# Launch the merge tool
git mergetool
Reducing Conflicts
| Practice | Effect |
|---|---|
| Merge main into feature branches daily | Small, frequent merges |
| Keep feature branches short-lived | Less divergence |
| Communicate with teammates | Avoid overlapping work |
| Use small, focused commits | Easier to resolve |
| Define clear ownership | Fewer people touch same files |
Complex Conflict Resolution
Sometimes a conflict involves structural changes:
# If a file was deleted in one branch and modified in another
git add/rm the file based on intent
# If you need both branches' changes
# Edit manually to combine them
# Binary file conflicts
git checkout --ours logo.png # Keep your version
git checkout --theirs logo.png # Keep their version
Practice
# Create a conflict on purpose
mkdir conflict-lab && cd conflict-lab
git init
echo "initial" > file.txt && git add . && git commit -m "init"
git checkout -b feature-a
echo "feature A change" > file.txt
git add . && git commit -m "feature A"
git checkout main
echo "main change" > file.txt
git add . && git commit -m "main change"
git merge feature-a
# CONFLICT!
# Now resolve it in your editor
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro