DEV Community

Abhishek Gupta
Abhishek Gupta

Posted on

πŸš€ Git Merge Conflicts Explained (0 Monster Guide with Real Example)

β€œYour code works… until you try to merge πŸ˜…β€

If you’ve ever seen this:

CONFLICT (content): Merge conflict in file.js
Enter fullscreen mode Exit fullscreen mode

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/CommDesk branch: feat/member-dashboard-refactor-v2

πŸ‘‰ Goal:

Merge feature branch into dev


❌ Common Mistakes (Very Important)

❌ Trying this:

git add https://github.com/user/repo
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ WRONG β€” git add is only for files


❌ Using this URL:

https://github.com/user/repo/tree/branch
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This tells Git:

β€œTrack that repo also”


πŸ“₯ Step 2 β€” Fetch code

git fetch upstream
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This downloads code but does not change your files

Now you have:

upstream/feat/member-dashboard-refactor-v2
Enter fullscreen mode Exit fullscreen mode

🌿 Step 3 β€” Switch to your branch

git checkout dev
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ You are now working on dev


πŸ”€ Step 4 β€” Merge the branch

git merge upstream/feat/member-dashboard-refactor-v2
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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";
Enter fullscreen mode Exit fullscreen mode

Feature branch

const role = "user";
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Git gets confused 😡


⚠️ 5. Conflict Markers (What You See)

<<<<<<< HEAD
const role = "admin";
=======
const role = "user";
>>>>>>> feature-branch
Enter fullscreen mode Exit fullscreen mode

🧠 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
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Remove:

<<<<<<<
=======
>>>>>>>
Enter fullscreen mode Exit fullscreen mode

βœ… 7. Mark as Resolved

git add .
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ You’re telling Git:

β€œI fixed everything”


🧾 8. Complete the Merge

git commit -m "resolved merge conflict"
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This creates a merge commit


πŸš€ 9. Push Final Code

git push origin dev
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ 10. Visual Flow (Super Important)

[Feature Repo]
       ↓ (fetch)
[upstream branch]
       ↓ (merge)
[dev branch]
       ↓ (push)
[GitHub]
Enter fullscreen mode Exit fullscreen mode

⚑ 11. Pro Tips (Real Developer Tricks)

🧨 Cancel merge if stuck

git merge --abort
Enter fullscreen mode Exit fullscreen mode

πŸ” See conflicts

git status
Enter fullscreen mode Exit fullscreen mode

πŸ“‘ Check remotes

git remote -v
Enter fullscreen mode Exit fullscreen mode

🌿 See all branches

git branch -a
Enter fullscreen mode Exit fullscreen mode

🧠 12. Deep Understanding (Interview Gold)

πŸ‘‰ What happens internally?

When you run:

git merge branch-name
Enter fullscreen mode Exit fullscreen mode

Git:

  1. Finds common ancestor
  2. Compares both branches
  3. Tries auto merge
  4. 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:

  1. Open file
  2. Remove markers
  3. Choose correct code
  4. git add .
  5. 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)