Three-Way Merge Conflict Resolver
Operator: Abdulla Al Fahad. How tools are built and checked
A conflict marker block tells you that two people changed the same lines, not what to do about it. To decide you need three things side by side: what the line was before either of you touched it, what you changed it to, and what they changed it to. This page shows that triple for every conflict, merges the changes that do not overlap by itself, and keeps a running count of what is still unresolved.
Conflicts are located and shaped the way git merge-file does it, so a region on this page is the region Git would have shown, and the result can go straight back into a rebase or merge.
Key features
- Two inputs: the three versions (base, ours, theirs), or one file that already holds merge-style or diff3-style conflict markers, read back into conflicts
- Automatic merge of changes that touch different lines or that both sides made identically, and per-conflict choices: ours, theirs, both with either side first, the base, or a hand edit
- Previous and next unresolved buttons, bulk take-ours or take-theirs for the rest, and a leftover-marker check on the final text that also catches marker-looking lines from hand edits
- Options for CRLF versus LF, whitespace-insensitive matching, refined or whole-region conflict style, light syntax colouring, and a download or copy of the result
How conflicts are found and which Git rules are copied
The merge aligns base with ours and base with theirs using the diff3 algorithm from the node-diff3 library, then applies the rules git merge-file adds on top. In the default refined style a conflict whose two sides share lines is split around the shared lines, with the shared start and end moved out of the conflict, and conflicts separated by three or fewer unchanged lines are joined again. The diff3 style keeps whole regions and shows the base between ||||||| and =======.
A file with markers is read back by scanning for a run of at least seven < characters, optional | lines for the base, = lines and > lines of the same length, with the labels after them kept. An opening marker inside an open conflict, an end marker with no start and a conflict that is never closed are reported with their line number instead of guessed at. Limits: 8 MB per file and 200,000 lines. The merge runs in a background worker with a Cancel button, because the line diff grows roughly with the square of the file length and a file with many repeated lines can take a minute or more at the top of the range. The refinement step compares the two sides of one conflict with a table of up to four million cells, so an enormous conflict stays as one block. Line endings are compared as written unless the CRLF option is on, in which case both are read as LF and the result uses the ending you choose.
How it works
The base is compared with ours and with theirs to find each side's changed ranges. A stretch changed by only one side is taken as is. A stretch both sides changed identically is taken once. Where both sides changed overlapping ranges differently, the ranges are grown until they cover everything either side touched, and that region becomes one conflict with three columns: base, ours and theirs.
Which lines a region covers therefore depends on how the two diffs were aligned, not on what a person would call one logical edit. In the refined style, conflicts separated by at most three unchanged lines are joined into one, so edits three lines apart come out as a single conflict while the same edits four lines apart are two conflicts with the unchanged lines merged cleanly between them.
Common use cases
- A release manager merging a hotfix branch into a 2,500-line CHANGELOG sees every entry added at the top by both sides as one conflict whose base is empty, picks Both, ours first for it, and takes the rest automatically.
- A developer whose rebase stopped on package.json pastes the conflicted file into Conflict markers, takes theirs for the version line and both for two dependency lines, and downloads a file with 0 leftover markers.
- Two editors changed the same Markdown document offline. Pasting the original and both copies shows the two paragraphs both editors rewrote as conflicts and merges every other edit on its own, instead of a wall of colour in a diff.
How to use this tool
- Paste or load the base, ours and theirs, or switch to Conflict markers and paste the conflicted file. The built-in example fills in a small server.js.
- Choose Merge, walk through the conflicts with Next unresolved and pick a side, both, the base or Edit by hand for each; watch the Leftover markers number.
- Download the result or copy it.
Example
The built-in server.js, 13 lines in each version
Ours: adds const log = require('./log'), sets PORT = 8080 and replaces the final console.log with log('listening on ' + PORT)
Theirs: sets PORT = process.env.PORT || 3000 and HOST = '0.0.0.0', and adds res.setHeader('Content-Type', 'text/plain')After Merge
3 changes merged automatically (the require, the setHeader line and the new final log call)
1 conflict: ours has PORT = 8080 and HOST = 'localhost', theirs has PORT = process.env.PORT || 3000 and HOST = '0.0.0.0'
Choosing Both, ours first leaves 4 lines in that spot and 0 leftover markers; leaving it unresolved shows the conflict between <<<<<<< ours and >>>>>>> theirs, 3 marker linesTips for best results
- During a rebase, ours is the branch you are rebasing onto and theirs is your own commit being replayed, the reverse of a merge. Paste the three versions in that order, or the labels will point to the wrong person.
- Resolve with the base column in view: if one side equals the base, that side made no change and the other side's version is almost always the right one.
- Both, theirs first and both, ours first differ only in order. For lists of imports or dependency lines the order rarely matters; for statements it does.
Common pitfalls
A merge that has no conflicts is not necessarily correct: two changes that each work alone can break the code together, so run the tests on the result.
Where two sides repeat the same line many times, equally valid alignments exist and Git may place a conflict boundary a line or two differently than this page does.
Common errors and how to fix them
- Files over 200,000 lines are not merged here.
- Split the file at a clean boundary, such as a top-level section, merge the parts separately and join the results; a generated file of that size is usually better regenerated than merged.
- The conflict that begins at line 14 is never closed with >>>>>>>
- The text was cut off, or a marker line was edited. Add the missing closing marker, or paste the whole file again.
Best practices
- After resolving, search the result for the marker strings yourself too; the check here covers lines that start with them, not references inside a string.
- Resolve the smaller conflicts first and read the larger ones once the surrounding code is settled.
Frequently asked questions
Why do ours and theirs look swapped during a rebase?
Git replays your commits onto the target branch, so while it applies them the branch being built is ours and your own commit is theirs. The page uses the labels you give it, so if the conflicted file came from a rebase, read the labels in the markers rather than assuming the usual meaning.
What happens to CRLF line endings?
By default the page converts CRLF and lone CR to LF on input and writes the result with the line ending you choose, which defaults to that of the first file. Turn the option off to compare line endings as written, in which case a line that differs only in its ending is a change and can conflict.
Does it give the same result as git merge-file?
On 1,500 random merges of files whose lines are all distinct, in the refined style and labelled the way git labels them, the output was byte-for-byte the same as git merge-file -p. Files that repeat lines heavily matched in roughly nine cases out of ten; in the others an equally valid alignment put a conflict boundary a line or two away.
Why does Ignore whitespace keep the base text for whitespace-only differences?
When the only change between a version and the base is spacing, matching treats the lines as equal and the page has no changed line to take. The base line is the neutral choice; re-indent afterwards if you want a side's spacing.