What Causes Merge Conflicts
Auto-merge alag lines pe kaam karta hai. Same line pe conflict hoga. Frequent merge karo, chhote conflicts karo.
Git is smart, but it is not psychic. When changes happen in different files or different lines of the same file, Git merges them automatically — no human intervention needed.
A merge conflict occurs when two branches modify the SAME LINE in the SAME FILE in different ways. Git cannot decide which version is correct — it marks the conflict and asks you to choose.
Think of it like two editors rewriting the same paragraph of a document simultaneously. Both changes might be good, but they cannot both exist in the same line. Someone must decide.
Conflicts are normal in team development. They are not errors, mistakes, or signs of bad practice. They simply mean two people were working on the same code at the same time — which is what teams do.
The more people working on the same files, the more conflicts you will encounter. This is a natural consequence of collaboration.
# Scenario: No conflict (different lines)
# Branch A: changed line 5
# Branch B: changed line 10
git merge branch-b
# Auto-merged! No intervention needed.
# Scenario: CONFLICT (same line)
# Branch A: changed line 5 to "Feature A"
# Branch B: changed line 5 to "Feature B"
git merge branch-b
# CONFLICT (content): Merge conflict in app.js
# Automatic merge failed; fix conflicts and then commit.
Not all merges are equal. There are two fundamentally different types of merges in Git, and understanding the difference is key to understanding conflicts.
Fast-forward merge: The branch you are merging into has no new commits since you branched off. Git simply moves the pointer forward. No divergent history to combine = zero chance of conflict.
3-Way merge: The branch you are merging into has new commits since you branched off. Git must find the common ancestor and combine both histories. A new merge commit is created with two parent commits. Conflicts CAN happen here.
If main has no new commits since you created feature, Git does a fast-forward. If main has new commits, Git must do a 3-way merge: common ancestor + your changes + their changes.
Use git merge --no-ff to force a 3-way merge even when fast-forward is possible. This preserves the branch topology in your history, which is useful for understanding when and why branches were created.
# Fast-forward merge (no conflict possible)
git checkout main
git merge feature
# Fast-forward
# feature | 3 -------
# main moves to feature
# 3-way merge (conflict possible)
git checkout main
git merge feature
# Merge made by the "ort" strategy.
# If same lines changed: CONFLICT!
# Force 3-way merge (preserves branch topology)
git merge --no-ff feature
# Always creates a merge commit, even if fast-forward was possible.
--no-ff for feature branches to maintain a clear history of when features were integrated. Choose based on your team's needs.Git's auto-merge is surprisingly powerful. Most merges complete without any manual intervention because Git can combine non-overlapping changes intelligently.
Different files changed: auto-merge always succeeds. If Branch A modified auth.js and Branch B modified payment.js, Git simply takes both changes.
Same file, different functions/sections: auto-merge usually succeeds. If Branch A added a login() function and Branch B added a logout() function, Git combines both.
Same file, different lines within the same section: auto-merge succeeds. If Branch A changed lines 1-10 and Branch B changed lines 50-60, Git takes both changes.
The only time auto-merge fails: same lines, same file, different changes. Both branches modified the exact same line with different content. Git cannot choose which version to keep.
Even when auto-merge succeeds, ALWAYS review the merged code. Two changes that don't conflict textually might conflict logically. A function call and its definition might both be changed independently, creating a runtime error even though Git merged without complaints.
# Auto-merge success: different functions
# Branch A added function login()
# Branch B added function logout()
# Git merges both into the file automatically.
# Auto-merge success: different lines
# Branch A changed line 1-10
# Branch B changed line 50-60
# Git merges both changes.
# Auto-merge FAILS: overlapping changes
# Branch A changed line 5-15
# Branch B changed line 10-20
# Overlap! Git cannot decide which version to keep.
# Always verify auto-merged code
git diff HEAD # Review what Git auto-merged
# Sometimes Git merges correctly but the logic is broken!
Many developers fear merge conflicts. They treat conflicts like errors, something to be ashamed of. This mindset is wrong.
Conflicts mean two people were working on the code simultaneously — that is a healthy team. The alternative is one person doing everything, which is worse in every way.
Long-lived branches cause MORE and HARDER conflicts. The longer you wait to merge, the more both branches diverge, and the more likely you are to step on each other's changes.
Frequent merging reduces conflict size and complexity. When you merge daily, each merge has very few changes. Any conflicts are tiny and easy to resolve.
The best teams do not avoid conflicts — they resolve them quickly and communicate about overlapping work. Conflict resolution is a skill, not a punishment.
# BAD: Avoiding merges for weeks (massive conflict at the end)
git checkout -b feature
# ... 3 weeks of work ...
git checkout main
git merge feature
# 47 files have conflicts! Nightmare!
# GOOD: Frequent merges (small, manageable conflicts)
git checkout -b feature
# ... 1 day of work ...
git checkout main
git merge feature
# 0-2 conflicts, easy to resolve.
# The merge often, merge small principle:
# Merge daily = almost no conflicts
# Merge weekly = some conflicts
# Merge monthly = merge hell
Not all conflicts are created equal. Understanding the different types helps you resolve them correctly.
Content conflict: The most common type. Both branches modified the same line in the same file with different content. You must choose which version to keep (or combine them manually).
Add/Add conflict: Both branches added a new file with the same name but different content. You must decide which file to keep or how to combine them.
Delete/Modify conflict: One branch deleted the file, the other modified it. You must decide whether to keep the file (with modifications) or accept the deletion.
Rename conflict: One branch renamed the file, the other modified it. You must decide how to handle the rename alongside the modifications.
Each type requires a different resolution strategy. git status tells you the type of conflict for each file, which guides your approach.
# Content conflict (same line changed)
# CONFLICT (content): Merge conflict in app.js
# Add/Add conflict (both added same new file)
# CONFLICT (add/add): Merge conflict in new-feature.js
# Delete/Modify conflict (one deleted, one changed)
# CONFLICT (delete/modify): Merge conflict in old-module.js
# Check conflict details
git status
# Unmerged paths:
# both modified: app.js (content conflict)
# added by us: new-feature.js (add/add conflict)
# deleted by them: old-module.js (delete/modify conflict)
Lo kar liya — Key Points:
- ✅ Merge conflicts happen when two branches modify the SAME LINE in the SAME FILE differently
- ✅ Git auto-merges changes in different files or different lines — conflicts only occur on overlapping changes
- ✅ Fast-forward merges have zero conflict risk; 3-way merges can have conflicts
- ✅ Conflicts are normal in team development — they indicate active collaboration, not mistakes
- ✅ Frequent merging reduces conflict size and complexity; long-lived branches cause merge hell
- ✅ Types of conflicts: content (same line), add/add (same new file), delete/modify (one deleted, one changed)
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login