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
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.txtOptions
| Flag | What it does |
|---|---|
| -m | Output a merged file with conflict markers |
| -A | Show all changes, bracketing overlaps |
| -e | Output an ed script to apply YOURS onto MINE |
| -3 | Like -e but only for non-overlapping changes |
| exit 1 | Conflicts (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 "^<<<<<<<".