Diff3

diff3 is a Unix utility to compare three files and show any differences among them. diff3 can also merge files, implementing a three-way merge.

History and implementations
diff3 originally appeared in Version 7 Unix of 1979. A very similar version was found in Berkeley Software Distribution (BSD) and was inherited into OpenBSD and probably Solaris.

On most modern Unix-like systems, even BSD-derived ones like FreeBSD and NetBSD, the diff3 provided is GNU's version in diffutils (formerly GNU Tools). This version has the ability to merge files on its own without relying on ed. It internally invokes diff to do the comparison, but can use any other compatible tool too.

The command is available as a separate package for Microsoft Windows as part of the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities.

"Diff3" has also become a generic name for the three-way-merge algorithm, specifically one based on reconciling two different diffs stemming from the first source.

Merging from a common ancestor
When two people have made changes to copies of the same file, diff3 can produce a merged output that contains both sets of changes together with warnings about conflicts. diff3 can merge three or more sets of changes to a file by merging two change sets at a time.

diff3 can incorporate changes from two modified versions into a common preceding version. This enables users to merge the sets of changes represented by the two newer files. This can be enabled using a command like this:.

This is like subtracting the file  from the file   and adding the result to the file , or as merging into   the changes that would turn   into. This merging is well-defined as long as  and   match in the neighborhood of each such change. This fails to be true when all three input files differ or when only  differs; this is a conflict. When all three input files differ, the conflict is an overlap.

diff3 has several methods to handle overlaps and conflicts. It can omit overlaps or conflicts, or select only overlaps, or mark conflicts with special  and   lines.

diff3 can output the merge results as an ed script that can be applied to the first file to yield the merged output. However, directly generating the merged output bypasses some problems with ed.