0
0
Gitdevops~15 mins

Merge conflicts why they happen in Git - Deep Dive

Choose your learning style9 modes available
Overview - Merge conflicts why they happen
What is it?
Merge conflicts happen when two people change the same part of a file in different ways and Git cannot decide which change to keep. It is like two versions of a story that clash because they both changed the same sentence. Git stops and asks you to fix the conflict before continuing. This ensures that no changes are lost or overwritten by mistake.
Why it matters
Without understanding merge conflicts, developers might overwrite each other's work or create broken code. Merge conflicts protect the project by making sure changes are combined carefully. If conflicts were ignored, the project could become unstable or lose important updates, causing delays and frustration.
Where it fits
Before learning about merge conflicts, you should know basic Git commands like commit, branch, and merge. After mastering conflicts, you can learn advanced Git workflows, rebasing, and collaboration strategies to work smoothly in teams.
Mental Model
Core Idea
Merge conflicts occur when Git cannot automatically combine different changes made to the same part of a file by multiple people.
Think of it like...
Imagine two friends editing the same paragraph of a shared story on paper at the same time. When they try to combine their edits, they find the sentences clash and must decide together which version to keep.
┌───────────────┐       ┌───────────────┐
│ Branch A edit │       │ Branch B edit │
└──────┬────────┘       └──────┬────────┘
       │                       │
       │                       │
       └───────┬───────────────┘
               │
        Git tries to merge
               │
       ┌───────▼────────┐
       │ Merge conflict! │
       └─────────────────┘
Build-Up - 7 Steps
1
FoundationWhat is a merge in Git
🤔
Concept: Introduce the basic idea of merging branches in Git.
In Git, a merge combines changes from one branch into another. For example, if you have a main branch and a feature branch, merging brings the feature changes into main. Git tries to combine all changes automatically.
Result
The branches are combined if there are no conflicting changes.
Understanding merging is essential because conflicts only happen during this process.
2
FoundationHow Git tracks changes
🤔
Concept: Explain how Git records changes to files and lines.
Git tracks changes by comparing file snapshots and recording differences line by line. When merging, Git looks at the common ancestor and the changes made in each branch to decide how to combine them.
Result
Git can usually merge changes automatically if they affect different lines.
Knowing Git's line-by-line tracking helps explain why conflicts happen only when the same lines change.
3
IntermediateWhy conflicts happen on same lines
🤔Before reading on: do you think conflicts happen only when files differ, or only when the same lines differ? Commit to your answer.
Concept: Conflicts occur when the same lines in a file are changed differently in two branches.
If two branches change different lines, Git merges automatically. But if both change the same line differently, Git cannot decide which change to keep. This causes a merge conflict.
Result
Git stops the merge and marks the conflicting lines for you to fix.
Understanding that conflicts are about the same lines clarifies why some merges are smooth and others need manual help.
4
IntermediateConflicts in multiple files and types
🤔Before reading on: do you think conflicts only happen in code files or also in other file types? Commit to your answer.
Concept: Merge conflicts can happen in any file type, not just code, and can involve multiple files at once.
Git treats all files as text by default. If two branches change the same part of any file, like a config or documentation file, conflicts can occur. Multiple files can have conflicts in one merge.
Result
You may see several conflict markers across different files needing resolution.
Knowing conflicts are not limited to code helps prepare for real-world merges involving many file types.
5
IntermediateHow Git marks conflicts in files
🤔
Concept: Show how Git marks conflicts inside files for manual resolution.
When a conflict happens, Git inserts special markers in the file: <<<<<<< HEAD Your changes ======= Other branch changes >>>>>>> branch-name You edit these markers to choose or combine changes, then save the file.
Result
The conflicted file clearly shows both versions side by side for you to fix.
Seeing conflict markers helps you understand exactly what Git cannot merge automatically.
6
AdvancedWhy conflicts happen with renames and deletes
🤔Before reading on: do you think conflicts only happen with edits, or also with file renames and deletes? Commit to your answer.
Concept: Conflicts can also occur when files are renamed or deleted differently in branches.
If one branch renames a file and another edits or deletes it, Git may not know how to combine these changes. This causes conflicts that need manual resolution.
Result
Merge stops with conflict messages about file renames or deletions.
Understanding conflicts beyond line edits prepares you for complex merges involving file structure changes.
7
ExpertHow merge algorithms affect conflicts
🤔Before reading on: do you think all Git merges use the same method, or can algorithms differ? Commit to your answer.
Concept: Git uses different merge algorithms that affect how conflicts are detected and resolved.
Git has several merge strategies like recursive and resolve. The recursive strategy can do a three-way merge using the common ancestor. Some algorithms try to minimize conflicts by smarter detection, but complex changes still cause conflicts.
Result
The choice of algorithm can reduce or increase conflicts in tricky merges.
Knowing merge algorithms helps experts choose strategies to reduce conflicts in large projects.
Under the Hood
Git performs a three-way merge by comparing the common ancestor version of a file with the two branch versions. It tries to combine changes line by line. When both branches change the same line differently, Git cannot decide automatically and flags a conflict. Internally, Git stores these changes as snapshots and diffs, using its index to track merge states.
Why designed this way?
Git was designed to support distributed teams working independently. The three-way merge allows combining changes efficiently while preserving history. Conflicts force human judgment where automatic merging is unsafe, preventing silent data loss. Alternatives like locking files were rejected to keep collaboration flexible.
Common Ancestor
      │
 ┌────┴────┐
 │         │
Branch A  Branch B
 │         │
 └────┬────┘
      │
   Git tries to merge
      │
  ┌───▼────┐
  │ Success│ or
  │Conflict│
  └────────┘
Myth Busters - 4 Common Misconceptions
Quick: do you think merge conflicts mean Git is broken? Commit yes or no.
Common Belief:Merge conflicts happen because Git is broken or buggy.
Tap to reveal reality
Reality:Merge conflicts are a normal and expected part of collaboration, showing Git is protecting your work.
Why it matters:Thinking conflicts are errors can cause frustration and avoidance of merges, slowing down teamwork.
Quick: do you think conflicts only happen in code files? Commit yes or no.
Common Belief:Conflicts only happen in source code files.
Tap to reveal reality
Reality:Conflicts can happen in any text file, including documentation, configs, or scripts.
Why it matters:Ignoring conflicts in non-code files can break builds or deployments unexpectedly.
Quick: do you think conflicts always mean the same lines were changed? Commit yes or no.
Common Belief:Conflicts only occur when the exact same lines are changed.
Tap to reveal reality
Reality:Conflicts can also happen due to file renames, deletes, or complex overlapping changes.
Why it matters:Assuming conflicts are only line edits can cause confusion when merges fail for other reasons.
Quick: do you think merge conflicts can be fully avoided with better communication? Commit yes or no.
Common Belief:Good communication can completely prevent merge conflicts.
Tap to reveal reality
Reality:While communication helps, conflicts are inevitable in complex projects with many changes.
Why it matters:Expecting zero conflicts leads to unrealistic workflows and frustration when conflicts do occur.
Expert Zone
1
Some merge conflicts are semantic, not just textual, requiring understanding of code meaning to resolve correctly.
2
Git's rerere feature can remember how you resolved conflicts before and apply the same fix automatically next time.
3
Merge conflicts can be minimized by smaller, frequent merges and clear team branching strategies.
When NOT to use
Avoid relying solely on merges in very large projects with many simultaneous changes; consider rebasing or feature toggles to reduce conflicts. Also, locking files is an alternative in binary-heavy projects where merges are impossible.
Production Patterns
Teams use pull requests with automated checks to catch conflicts early. Continuous integration merges branches frequently to reduce conflict size. Some use Git rerere to speed up repeated conflict resolution.
Connections
Version Control Systems
Merge conflicts are a core challenge in all version control systems that support branching and merging.
Understanding conflicts in Git helps grasp similar issues in other systems like Mercurial or SVN.
Collaborative Writing
Merge conflicts are like disagreements in collaborative document editing when two people change the same sentence differently.
Recognizing this connection helps appreciate the need for conflict resolution in any shared work.
Conflict Resolution in Negotiations
Merge conflicts mirror negotiation conflicts where two parties have different views on the same issue and must find a compromise.
Knowing this analogy highlights the human aspect behind technical conflicts and the importance of communication.
Common Pitfalls
#1Ignoring conflict markers and committing files without fixing conflicts.
Wrong approach:git add conflicted_file.txt git commit -m "Merge with conflicts"
Correct approach:Edit conflicted_file.txt to resolve conflicts git add conflicted_file.txt git commit -m "Resolved merge conflicts"
Root cause:Misunderstanding that conflict markers must be manually removed before committing.
#2Trying to merge very large branches infrequently, causing many conflicts at once.
Wrong approach:git merge big-feature-branch (after weeks of separate work)
Correct approach:Merge smaller changes frequently to reduce conflict size and complexity.
Root cause:Not realizing that frequent merges keep conflicts manageable.
#3Assuming conflicts only happen in code and ignoring conflicts in config or docs.
Wrong approach:git merge # conflicts in config.yaml ignored or overwritten
Correct approach:Check and resolve conflicts in all files, not just code.
Root cause:Believing only code files matter in merges.
Key Takeaways
Merge conflicts happen when Git cannot automatically combine different changes to the same part of a file.
They protect your work by forcing you to decide how to combine conflicting changes safely.
Conflicts can occur in any text file and also involve renames or deletes, not just line edits.
Understanding how Git tracks changes and merges helps you predict and resolve conflicts effectively.
Frequent merges and good team communication reduce conflict size but cannot eliminate conflicts entirely.