Skip to content
Latchkey

diff3: Three-Way Merge and Compare

diff3 compares three files, typically two edited versions and their common ancestor, and can emit a merged file with conflict markers.

diff3 is the engine behind three-way merges. Understanding it helps you reason about merge conflicts and script automated merges of generated files.

What it does

diff3 takes three files (MINE, OLDER, YOURS) and reports how MINE and YOURS each changed relative to the common ancestor OLDER. With -m it produces a merged output, inserting <<<<<<< / ======= / >>>>>>> conflict markers where the two sides changed the same region.

Common usage

Terminal
diff3 mine.txt base.txt yours.txt
# produce a merged file with conflict markers
diff3 -m mine.txt base.txt yours.txt > merged.txt
# show only the merged result, ancestor-annotated
diff3 -A mine.txt base.txt yours.txt

Options

FlagWhat it does
-mOutput a merged file with conflict markers
-AShow all changes, bracketing overlaps
-eOutput an ed script to apply YOURS onto MINE
-3Like -e but only for non-overlapping changes
exit 1Conflicts (overlaps) were found

In CI

diff3 -m lets a pipeline attempt an automatic three-way merge of a generated file and detect conflicts by exit code. If the merged output contains <<<<<<< markers, the merge is not clean; grep for the marker and fail the job rather than committing conflicted content.

Common errors in CI

Conflict markers <<<<<<<, =======, >>>>>>> left in a file mean the two sides changed the same lines and diff3 could not merge them; that is the signal to resolve manually. Exit status 1 from diff3 means overlaps were found, exit 2 means an error (bad file). Committing a file that still contains merge markers is the most common downstream failure, so gate on grep -n "^<<<<<<<".

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →