Resolving Merge Conflicts — A Step-by-Step Practical Guide
In this tutorial, you'll learn about Resolving Merge Conflicts. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
What You'll Learn
Resolve Git merge conflicts step by step — interpret conflict markers, use merge tools, abort when needed, and adopt workflows that minimize conflict frequency.
Why It Matters
Merge conflicts are inevitable when multiple developers edit the same files. Understanding how to resolve them calmly and correctly is the difference between a 2-minute fix and a 30-minute panic that breaks the build.
Real-World Use
Two developers modified the same function — one added error handling, the other changed the return type. Git cannot merge automatically and flags a conflict. With the right approach, you merge both changes in under a minute.
What Causes Merge Conflicts?
A conflict happens when Git cannot automatically reconcile changes from two branches:
Developer A (branch: feature/payments):
def process_payment(amount):
return amount * 1.05 # added tax
Developer B (branch: feature/discounts):
def process_payment(amount, coupon):
return amount * 0.9 # added 10% off
Git merge: conflict — both changed the same lines differently.
graph LR
A[Branch: main] --> B[feature/payments]
A --> C[feature/discounts]
B --> D[Merge into main]
C --> D
D --> E{Conflict?}
E -->|No| F[Automerge success]
E -->|Yes| G[Conflict markers in file]
G --> H[Resolve manually]
H --> I[git add & git commit]
Understanding Conflict Markers
# After git merge fails, open the conflicted file
cat src/payment.py
# <<<<<<< HEAD
# def process_payment(amount):
# return amount * 1.05
# =======
# def process_payment(amount, coupon):
# return amount * 0.9
# >>>>>>> feature/discounts
The markers mean:
<<<<<<< HEAD— start of your current branch's version=======— divider between the two versions>>>>>>> feature/discounts— end of the incoming branch's version
Resolving from the Command Line
# Start a merge that results in a conflict
git checkout main
git merge feature/discounts
# Auto-merging src/payment.py
# CONFLICT (content): Merge conflict in src/payment.py
# Automatic merge failed; fix conflicts and then commit the result.
# View conflicted files
git status
# both modified: src/payment.py
# Open the file and resolve manually
# Edit to combine both changes:
# def process_payment(amount, coupon=None):
# total = amount * 1.05 # tax
# if coupon:
# total *= 0.9 # discount
# return total
# Mark as resolved
git add src/payment.py
# Complete the merge
git commit -m "Merge feature/discounts: combine tax and discount logic"
Using a Merge Tool
# Configure a merge tool (vimdiff, meld, kdiff3, etc.)
git config merge.tool vimdiff
git config merge.conflictstyle diff3 # Shows base version too
# Launch the merge tool
git mergetool
# Three panels: local (yours), base (common ancestor), remote (theirs)
# After resolving in the tool, save and exit
# Git automatically stages the resolved file
# Complete the merge
git commit -m "Merge feature/discounts with resolved conflicts"
Aborting a Merge
# If the conflict is too complex, abort entirely
git merge --abort
# Returns to the state before the merge started
Preventing Conflicts
# 1. Pull before you start working
git checkout feature/payments
git pull origin main # Merge latest main into your feature
# 2. Rebase instead of merge for feature branches
git checkout feature/payments
git rebase main # Replay your commits on top of main
# 3. Communicate with your team about which files you're editing
# 4. Keep feature branches short-lived (merge every 1-2 days)
Resolving Binary File Conflicts
# Binary files (images, PDFs) can't be merged — Git asks you to pick one
git checkout --ours src/logo.png # Keep the version from your branch
git checkout --theirs src/banner.png # Keep the incoming version
git add src/logo.png src/banner.png
git commit -m "Merge: resolve binary conflicts"
Common Pitfalls
| Mistake | Solution |
|---|---|
Forgetting to git add after resolving |
Git won't let you commit unmerged files — run git status to check |
| Resolving incorrectly by keeping only one side | Use diff3 style to see the base version and combine both changes |
| Force-pushing a conflicted merge | Never force-push merge commits — use git merge --abort and redo |
| Ignoring whitespace conflicts | Use git merge -Xignore-space-change to auto-resolve whitespace |
Practice Questions
- What do the three sections of a conflict marker mean (using
diff3style)? - How do you abort a merge that has unresolved conflicts?
- What workflow changes can reduce the frequency of merge conflicts?
FAQs
Next Steps
After mastering Conflict Resolution, learn version control strategies to minimize them, or explore advanced stash techniques to save work-in-progress without committing.
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro