Merge Conflicts
What you'll learn: You'll understand why Git sometimes can't automatically merge branches and needs your help to resolve conflicting changes.
What Causes a Merge Conflict?
Remember how three-way merges automatically combine changes from different branches? That works beautifully—until two branches modify the exact same lines in the same file. When this happens, Git doesn't know which version to keep, so it stops and asks you to decide.
Think of it like two editors working on the same paragraph of a document. If one editor rewrites the opening sentence while another editor also rewrites that same opening sentence differently, there's no obvious "correct" combination. A human needs to choose which version wins, or blend them together.
When Git Can't Decide
A merge conflict occurs when:
- Two branches change the same line(s) in a file
- One branch deletes a file while another modifies it
- Both branches add different content at the same location
Git is smart enough to merge changes in different parts of a file automatically. But when changes overlap in the same spot, Git pauses the merge, marks the conflicting sections in your files, and waits for you to manually resolve them.
The Result
When a conflict happens, Git leaves your repository in a special "merging" state. The conflicted files contain both versions with special markers showing where the disagreement is. You'll need to edit these files, choose (or combine) the correct changes, stage them with git add, and complete the merge with git commit.
Key Takeaway: Merge conflicts happen when Git encounters overlapping changes in the same file locations and cannot automatically determine which version to keep—requiring manual human intervention to resolve.