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 [OPTIONS] FILE1 FILE2 FILE3
FILE1 is typically the common original base file.
FILE2 is typically 'your' modified version.
FILE3 is typically 'their' modified version.

PARAMETERS

-A, --show-all
    Output all changes (common, non-overlapping, and conflicting). Conflict markers include the base content (FILE1) in the middle section.

-E, --ed
    Output an ed-style script that, when applied, merges the files. This output does not contain conflict markers but rather ed commands.

-L LABEL, --label=LABEL
    Use LABEL instead of the filename in the conflict headers. This option can be specified up to three times, applying to FILE1, FILE2, and FILE3 respectively.

-m, --merge
    Output the merged file. This is the default behavior and implies the standard conflict marker format.

-x, --overlap-only
    Output only the conflicting changes (overlapping edits). No base content is shown in conflict markers.

-X, --show-overlap
    Output conflicting changes, including the base content (FILE1) of the overlapping section, but exclude non-overlapping changes.

-V, --version
    Display version information and exit.

-H, --help
    Display a help message and exit.

DESCRIPTION

diff3 compares three versions of a file, identifying differences and attempting to merge them. It's commonly used in version control systems to resolve conflicts when multiple developers modify the same base file. The three files typically represent: BASE (the common ancestor), MINE (your version with changes), and THEIRS (another developer's version with changes).

The command attempts to combine changes made independently from the BASE. Where changes don't overlap, diff3 integrates them seamlessly. When changes overlap in a way that diff3 cannot automatically resolve (i.e., both MINE and THEIRS modified the same section differently from BASE), it inserts conflict markers. These markers delineate the conflicting sections, showing the versions from MINE and THEIRS, along with the BASE content if the -A or -X option is used. Users must then manually resolve these conflicts by editing the output file.

CAVEATS

diff3 assumes that FILE1 is the common ancestor of FILE2 and FILE3. If this assumption is incorrect, the merge results may be misleading or incorrect.

Automatic conflict resolution is limited; it handles only non-overlapping changes. Many conflicts require manual intervention and a clear understanding of the content. The tool operates line by line and does not understand the semantic meaning of changes in programming languages or other structured data.

CONFLICT MARKERS

When conflicts occur, diff3 inserts special markers into the output file to delineate the conflicting sections. The most common format (with -A or -X) is:

<<<<<<< LABEL_FILE2
Content from FILE2
||||||| LABEL_FILE1
Content from FILE1 (BASE)
=======
Content from FILE3
>>>>>>> LABEL_FILE3

The user must manually edit the section between <<<<<<< and >>>>>>> to resolve the conflict.

EXIT STATUS

diff3 returns an exit status to indicate the outcome of the merge:
0: No conflicts were found; the merge was clean.
1: Conflicts were found and inserted into the output.
2: Trouble occurred (e.g., file not found, invalid options).

HISTORY

diff3 is part of the diffutils package, a suite of programs for finding differences between files, which has been a staple of Unix-like operating systems since their early development. Its origins can be traced back to the original Unix diff utility. diff3 specifically addresses the problem of merging three versions of a file, a common requirement in collaborative software development and version control systems. Its design and algorithms have remained largely consistent, providing a foundational text merging tool that integrates well with systems like CVS and Subversion, and often serves as a component for Git's merge strategies.

SEE ALSO

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

Copied to clipboard