LinuxCommandLibrary

diff3

Compare three files, showing differences

TLDR

Compare files

$ diff3 [path/to/file1] [path/to/file2] [path/to/file3]
copy

Show all changes, outlining conflicts
$ diff3 [[-A|--show-all]] [path/to/file1] [path/to/file2] [path/to/file3]
copy

SYNOPSIS

diff3 [OPTION]... MYFILE OLDFILE YOURFILE

PARAMETERS

-a, --text
    Treat all files as text.

-e, --ed-script
    Output ed scripts for differences between OLDFILE and YOURFILE.

-E, --show-overlap
    Like -e, include overlapping lines.

-x, --overlap-only
    Like -e, output only overlapping lines.

-X
    Like -x, exclude lines matching MYFILE.

-3, --easy-only
    Like -e, only lines differing in both from OLDFILE.

-m, --merge
    Output merged file instead of ed script.

-A, --show-all
    Output all changes with markers (default with -m).

-L LABEL, --label=LABEL
    Label sections with LABEL instead of filenames.

-i SUFFIX, --suffix=SUFFIX
    Append SUFFIX to temporary merged filenames.

-T, --initial-tab
    Prepend tab to align tabs in output.

--diff-program=PROGRAM
    Use PROGRAM for pairwise diffs.

--help
    Print help.

--version
    Print version.

DESCRIPTION

diff3 is a command-line utility from the GNU diffutils package that compares three versions of a text file: MYFILE (your edited version), OLDFILE (common ancestor), and YOURFILE (other edited version). It identifies differences and generates either ed-compatible scripts or a merged output with conflict markers.

By default (-A or --show-all), it outputs all changes, marking sections where YOURFILE differs from OLDFILE with '2a' (added), '3f' (forwarded), or overlap markers like:
||||||| part of OLDFILE
=======
<<<<<<< MYFILE
[your content]
>>>>>>> YOURFILE

The -m option produces a full merged file suitable for human review, commonly used in version control for resolving conflicts. Other modes like -e output minimal ed scripts for automated application, -3 handles 'easy' cases (changes in both but non-overlapping), and -x focuses on overlaps.

diff3 processes files line-by-line, classifying hunks as unique to one file or conflicting. It's efficient for source code merging but expects textual input; use -a for binary. Widely used in scripts, RCS, early CVS, and as a basis for modern tools like Git's merge.

CAVEATS

Assumes line-oriented text; binaries may garble without -a. No charset conversion. Conflicts require manual resolution.

EXIT STATUS

0: success; 1: some hunks unmerged; 2: error or fatal.

EXAMPLE

diff3 -m -L "mine" -L "ancestor" -L "theirs" mine ancestor theirs > merged
Creates labeled merge with conflicts.

HISTORY

Part of GNU diffutils since 1988, developed by Randy McNally. Extends Unix diff (McIlroy, 1970) for three-way merges; POSIX standardized.

SEE ALSO

diff(1), merge(1), sdiff(1), patch(1)

Copied to clipboard