diff3 is a specific tool designed for comparing three different files, where each file represents a version: the original file, the first modified file, and the third modified file. It compares the files thoroughly, line by line, and shows the differences between them. It's particularly handy when two or more developers have made different changes to the same file.
Internally, diff3 utilizes the diff command for the comparison process. Some of the key features of diff3 command include three-way comparison, conflict identification, merge conflict resolution, and a flexible output format.
Syntax
diff3 [OPTION]... MYFILE OLDFILE YOURFILE
Where,
MYFILE
: Current version of the file.OLDFILE
: Original file before any changes were made.YOURFILE
: Another file that may contain modifications to the original file.
Some of the useful options of diff3 are:
-m
,--merge
: Outputs a merged version of the files, showing all changes.-A
,--show-all
: Outputs all changes, including conflicts. It brackets conflicts within the output, making it easier to identify and understand overlapping changes.-E
,--show-overlap
: Similar to-A
, but it focuses specifically on overlapping changes (conflicts). It brackets these conflicts for clearer visibility.-3
,--easy-only
: This option includes only non-overlapping changes in the output, effectively ignoring conflicts.-x
,--overlap-only
: Opposite to-3
, this option focuses exclusively on overlapping changes.-e
,--ed
: Generates aned
script for incorporating changes from one file to another.-a
,--text
: Treats all files as text.--strip-trailing-cr
: Strips trailing carriage return characters from input.
Basic Usage
Suppose you have three text files: original.txt, modified1.txt, and modified2.txt.
orignal.txt:
Line 1
Line 2
Line 3
modified1.txt
Line 1 modified by User 1
Line 2
Line 3
modified2.txt
Line 1
Line 2 modified by User 2
Line 3
Now, we can execute the following diff3 command which compares these files line by line and highlights the differences between them.
$ diff3 original.txt modified1.txt modified2.txt
Output
====
1:1,2c
Line 1
Line 2
2:1,2c
Line 1 modified by User 1
Line 2
3:1,2c
Line 1
Line 2 modified by User 2
Understanding the output:
This marker ====
: indicates that all three files are different at the particular point in the comparison. If modified1.txt is different from the other two files then would have shown line as ====2.
1:1,2c
, 2:1,2c
, and 3:1,2c
are change notations. These notations are formatted as [file number]:[line range in the file]c
. The c
stands for 'change'.
1:1,2c
refers to a change in lines 1 to 2 in original.txt.2:1,2c
refers to a change in lines 1 to 2 in modified1.txt.3:1,2c
refers to a change in lines 1 to 2 in modified2.txt.
Show merged changes
The -m option attempts to merge the changes from the three files, highlighting the conflicts that need manual resolution.
Example:
$ diff3 -m original.txt modified1.txt modified2.txt
Output
<<<<<<< original.txt
Line 1
Line 2
||||||| modified1.txt
Line 1 modified by User 1
Line 2
=======
Line 1
Line 2 modified by User 2
>>>>>>> modified2.txt
Line 3
Here the sections between the <<<<<<<
and >>>>>>>
markers indicate the actual conflicting parts of the files.
To resolve the conflict in the merged output, you need to manually decide which version of each conflicting line to keep.
Show only overlapping changes
To display only the overlapping changes (conflicts) among the three files being compared use -x option
$ diff3 -x original.txt modified1.txt modified2.txt
Output
1,2c
Line 1
Line 2 modified by User 2
.
The conflict identified by -x
is likely due to the fact that Line 2
in original.txt and modified1.txt is different from Line 2 modified by User 2
in modified2.txt.
The first line does not constitute a conflict since original.txt
and modified2.txt
have the same content ("Line 1"), and thus, the change is only in modified1.txt
.
Show only non overlapping changes
With the -3 and -m options, diff3 shows the non-overlapping (non-conflicting) changes from the files being compared.
Example:
$ diff3 -3 -m original.txt modified1.txt modified2.txt
Output
Line 1
Line 2
Line 3
This means it will display only those parts of the files that are identical among all three.
Show all changes
The -A or --show-all option outputs all changes and brackets conflicts within the files being compared.
Example:
$ diff3 -A original.txt modified1.txt modified2.txt
Output:
2a
||||||| modified1.txt
Line 1 modified by User 1
Line 2
=======
Line 1
Line 2 modified by User 2
>>>>>>> modified2.txt
.
0a
<<<<<<< original.txt
.
Where the 0a
followed by <<<<<<< original.txt
and a .
implies no changes or conflicts in original.txt at the beginning of the file.
Treat all files as text
By default diff3 does not accept binary files, It fails to compare with other files.
$ diff3 -m original.txt modified1.txt document
diff3: diff failed: Binary files document and modified1.txt differ
Now to treat all files as text files, you can use -a flag. Example:
$ diff3 -m -a original.txt modified1.txt document.bin
<<<<<<< original.txt
Line 1
||||||| modified1.txt
Line 1 with some changes.
=======
3m��ek���7�|��}~�)�'�pmʌi��X2�UI;8�B.ˆ`���1B�b�\|�>�T�F��\
�U��y��}Wc�d`2JkT���eʅ�)�
�,'���b�20:�A�-Zo�����r)
Line 1
>>>>>>> document.bin
Line 2
Line 3 modified
�X�GYbw������?���
Accept input stdin
Using the diff3 command with standard input (stdin) instead of a file is a flexible feature that allows you to compare changes directly from the input you provide in the command line.
Example:
$ diff3 -m original.txt modified1.txt -
Input your changes directly in the command line then Press CTRL+D to signal the end of input. The output will show a merged view of original.txt, modified1.txt, and your stdin input.
Using with git
The diff3 format can be extremely useful for resolving merge conflicts in Git.
To set Git to use the diff3 style for merge conflicts globally, you can use the following command:
git config --global merge.conflictstyle diff3
This is especially useful when you're dealing with merge conflicts, as it provides more context, helping to better understand and resolve the conflicts.
Comments