βYour code worksβ¦ until you try to merge π β
If youβve ever seen this:
CONFLICT (content): Merge conflict in file.js
and thought:
βWhat did I just break??β
This guide will take you from zero β pro level understanding of Git merge conflicts, using a real-world scenario with two repos.
π§ 1. The Real Problem (What We Faced)
We had:
- π’ Main Repo β
NexGenStudioDev/CommDesk(branch:dev) - πΏ Feature Repo β
Shreyashi-77/CommDeskbranch:feat/member-dashboard-refactor-v2
π Goal:
Merge feature branch into
dev
β Common Mistakes (Very Important)
β Trying this:
git add https://github.com/user/repo
π WRONG β git add is only for files
β Using this URL:
https://github.com/user/repo/tree/branch
π WRONG β this is a web page, not a Git repo
π§ 2. Core Concept (Must Understand First)
π Git has 3 main ideas:
1. origin
π Your repo
2. upstream
π Someone elseβs repo (fork)
3. Branch
π Different versions of code
π 3. Step-by-Step (Correct Way)
β Step 1 β Add the other repo
git remote add upstream https://github.com/Shreyashi-77/CommDesk.git
π This tells Git:
βTrack that repo alsoβ
π₯ Step 2 β Fetch code
git fetch upstream
π This downloads code but does not change your files
Now you have:
upstream/feat/member-dashboard-refactor-v2
πΏ Step 3 β Switch to your branch
git checkout dev
π You are now working on dev
π Step 4 β Merge the branch
git merge upstream/feat/member-dashboard-refactor-v2
π Git tries to combine both codes
π₯ 4. What is a Merge Conflict?
A conflict happens when:
Same file + same line changed in both branches
π§ͺ Example
Your branch (dev)
const role = "admin";
Feature branch
const role = "user";
π Git gets confused π΅
β οΈ 5. Conflict Markers (What You See)
<<<<<<< HEAD
const role = "admin";
=======
const role = "user";
>>>>>>> feature-branch
π§ Meaning:
| Marker | Meaning |
|---|---|
HEAD |
Your current branch |
| bottom part | Incoming code |
π οΈ 6. How to Fix (Manually)
Edit the file:
const role = "admin"; // or "user" or combine
π Remove:
<<<<<<<
=======
>>>>>>>
β 7. Mark as Resolved
git add .
π Youβre telling Git:
βI fixed everythingβ
π§Ύ 8. Complete the Merge
git commit -m "resolved merge conflict"
π This creates a merge commit
π 9. Push Final Code
git push origin dev
π₯ 10. Visual Flow (Super Important)
[Feature Repo]
β (fetch)
[upstream branch]
β (merge)
[dev branch]
β (push)
[GitHub]
β‘ 11. Pro Tips (Real Developer Tricks)
𧨠Cancel merge if stuck
git merge --abort
π See conflicts
git status
π‘ Check remotes
git remote -v
πΏ See all branches
git branch -a
π§ 12. Deep Understanding (Interview Gold)
π What happens internally?
When you run:
git merge branch-name
Git:
- Finds common ancestor
- Compares both branches
- Tries auto merge
- If fails β conflict
π Why conflicts happen?
- Same file edited
- Same line changed
- Git cannot decide
βοΈ 13. Merge vs Rebase (Quick)
| Merge | Rebase |
|---|---|
| Keeps history | Linear history |
| Safer | Cleaner |
| Creates merge commit | No merge commit |
π Beginners β use merge
π§© 14. Mental Model (Never Forget)
π Think Git like this:
-
remoteβ connection -
fetchβ download -
mergeβ combine -
conflictβ manual decision
π― 15. Final Summary
π Merge conflict = Git asking for help
π Solution:
- Open file
- Remove markers
- Choose correct code
git add .git commit
π¬ 16. Real Advice
βConflicts are not errors β they are collaboration decisions.β
The better you understand them, the faster you grow as a developer π
π Bonus
If you want next post:
- π₯ Git Rebase (0 β Advanced)
- π₯ Docker + Git workflow
- π₯ How to avoid conflicts completely
Just comment βNEXTβ π
Top comments (0)