Developer tools
Merge conflict
A place where two branches changed the same thing differently, and git asks a human to decide what to keep.
Why it exists
Two branches can be merged automatically only if they touched different places. If you set the discount to 10% and a teammate set it to 15% in their branch, a machine can't know the right answer: maybe it's one of them, maybe a third value. Silently picking "the newer one" would mean quietly losing someone's work. So git stops and shows both versions right in the file — the decision is a human's.
Where it's used
git mergeand git pull, when you and a teammate changed the same line.git rebaseandgit cherry-pick— the same conflicts, just while moving commits one at a time.- A GitHub pull request marked "This branch has conflicts": it can't be merged until the conflict is resolved.
- The "Accept Current / Incoming / Both" buttons in VS Code and other editors.
Syntax
<<<<<<< HEAD
your version (current branch)
=======
incoming version
>>>>>>> branch-nameExamples
const text = [
"<<<<<<< HEAD",
"const discount = 0.1;",
"=======",
"const discount = 0.15;",
">>>>>>> new-cart",
].join("\n");
const lines = text.split("\n");
const middle = lines.indexOf("=======");
lines.slice(1, middle); // → ["const discount = 0.1;"]
lines.slice(middle + 1, -1); // → ["const discount = 0.15;"]function mergeLine(base, ours, theirs) {
if (ours === theirs) return ours;
if (ours === base) return theirs;
if (theirs === base) return ours;
return "CONFLICT";
}
mergeLine("10%", "10%", "15%"); // → "15%" — only they changed it
mergeLine("10%", "12%", "10%"); // → "12%" — only we changed it
mergeLine("10%", "12%", "15%"); // → "CONFLICT" — both changed it, differentlyconst hasMarkers = (text) => /^(<<<<<<<|=======|>>>>>>>)/m.test(text);
hasMarkers("const a = 1;\n<<<<<<< HEAD\nconst b = 2;"); // → true
hasMarkers("const a = 1;\nconst b = 2;"); // → falseResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
Git does a three-way merge: it finds the branches' common ancestor (the merge base) and compares both versions of each fragment with it. If only one side changed it, that side's version wins. If both changed it the same way, no questions either. If both changed it differently — a conflict.
Git writes both versions into the file: from <<<<<<< HEAD to ======= is yours (the current branch), from ======= to >>>>>>> branch is the incoming one. The file is marked as conflicted and the merge waits: you tidy the file up, remove all the markers, stage it (git add) and finish the merge with a commit. Changed your mind? git merge --abort puts everything back the way it was before the merge.
Traps
How it differs from…
- Git: commits, branches, history
A conflict isn't a git error but an honest question: git merges everything it can on its own and stops only where the decision belongs to a human.
When to pick something else
The best conflict is the one that never happened: keep branches short-lived, pull in main often, and don't reformat a whole file along with a small fix — reformatting conflicts with everyone.