Version control
What is a Merge Conflict?
Definition
A merge conflict occurs when two branches changed the same part of a file and the version control system cannot determine which change should win.
Also known as: Conflict
Merge Conflict in more detail
Git merges automatically whenever changes touch different regions, which is most of the time. It stops when two branches modified overlapping lines, or when one deleted a file the other edited, because resolving that requires knowing what the changes meant. Rather than guess, Git marks the file, pauses the merge and hands the decision to you.
A conflicted file contains both versions between markers: your current branch's content after the row of less-than signs, the incoming branch's content after the equals signs, ending at the greater-than signs. Resolving means editing the file into the state it should actually be — which is often neither side verbatim but a combination — then deleting every marker, staging the file and completing the merge. Committing a marker into the repository is a rite of passage and a broken build.
The conflicts that hurt are the ones the tool cannot see. If one branch renames a function and another adds a caller for the old name, the merge succeeds cleanly and the build breaks: a semantic conflict. This is why the real defence is not better resolution skill but smaller, more frequent merges — plus running the test suite after every merge, since a clean merge is not evidence of a correct one.
Merge Conflict example
$ git merge feature/search
Auto-merging src/search.js
CONFLICT (content): Merge conflict in src/search.js
<<<<<<< HEAD (what is on your current branch)
const LIMIT = 20;
======= (what is coming in)
const LIMIT = 50;
>>>>>>> feature/search
Resolve by editing the file into what it should be, then
$ git add src/search.js
$ git merge --continue # or: git commit
Escape hatches
git merge --abort return to the pre-merge state
git checkout --ours <f> keep this branch's version wholesale
git checkout --theirs <f> keep the incoming version wholesale
git rerere reuse recorded resolutions next time
A clean merge is not a correct merge. Run the tests.Merge Conflict — common questions
- How do I fix a merge conflict?
- Open each conflicted file, replace the marked region with the content that should exist, remove all conflict markers, then git add the file and finish with git merge --continue or git commit. Running the tests afterwards is part of the fix.
- Can I undo a merge that went wrong?
- Before committing, git merge --abort restores the pre-merge state exactly. After committing, git revert on the merge commit undoes it with a new commit, and git reflog will find where you were if you need to go further back.
- How do I avoid merge conflicts?
- Merge often and keep branches short-lived, so the two sides never drift far apart. Pulling main into a long-running branch regularly turns one enormous conflict into several small ones, which are far easier to reason about.
Keep reading
Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.
Browse the A-Z glossary