Merge Conflicts: What They Are
You'll learn: why Git cannot automatically merge branches when the same lines have been changed differently.
The Core Problem
A merge conflict occurs when Git tries to combine two branches but finds incompatible changes to the same line(s) in the same file. Git is smart enough to automatically merge many changes—like edits to different files or different parts of the same file—but when two branches modify identical lines, Git cannot decide which version to keep. It stops and asks you to resolve the conflict manually.
Why Conflicts Happen
Think of it like two editors working on the same paragraph of a document. If one editor rewrites the opening sentence while another deletes it entirely, there's no obvious "correct" combined version. Git faces the same dilemma.
Conflicts arise during three-way merges when:
- Branch A changes line 42 of
analysis.pyone way - Branch B changes that same line 42 a different way
- Git has no rules for deciding which change wins
Git can automatically merge changes to different lines or different files, but overlapping line changes require human judgment.
What Happens During a Conflict
When you attempt a merge (like merging your feature branch into main), Git will:
- Successfully merge all non-conflicting changes
- Stop the merge process when it encounters conflicts
- Mark the conflicting sections in your files
- Leave the repository in a "merging" state until you resolve and commit
Your working directory now contains files with special conflict markers showing both versions, and git status will indicate which files need resolution.
Key Takeaway: Merge conflicts happen when the same lines are edited differently in two branches, forcing you to manually choose or combine the changes because Git cannot make that decision automatically.