Linux merge command

Updated: 06/16/2017 by Computer Hope

About merge

merge is part of the RCS version control package. It is used to perform a three-way file merge.

merge analyzes three files -- an original file, and two modified versions of the original -- and compares them, line-by-line, attempting to resolve the differences between the two sets of modifications in order to create a single, unified file which represents both sets of changes.

Depending on the differences between the two sets of changes, this may be an automatic process or may require user input.

If neither set of changes conflicts with the other, merge can usually figure out what to do on its own. But if the two sets of changes conflict — for example, if the same line of text is worded differently in both modified files — merge will show the conflict in the resulting merged file.

Description

merge incorporates all changes that lead from file2 to file3 into file1. The result ordinarily goes into file1.

Suppose file2 is the original, and both file1 and file3 are modifications of file2. Then merge combines both changes.

A conflict occurs if both file1 and file3 have changes in a common segment of lines. If a conflict is found, merge normally outputs a warning and brackets the conflict with "<<<<<<<" and ">>>>>>>" lines. For instance, a typical conflict will look like this:

<<<<<<< file A
lines in file A
=======
lines in file B
>>>>>>> file B 

If there are conflicts, the user should edit the result and delete one of the alternatives.

merge syntax

merge [options] file1 file2 file3

Options

-A Output conflicts using the -A style of diff3(1) (if supported by diff3). This merges all changes leading from file2 to file3 into file1, and generates the most verbose output.
-E, -e These options specify conflict styles that generate less information than -A. See diff3(1) for details. The default is -E. With -e, merge does not warn about conflicts.
-L label This option may be given up to three times, and specifies labels to be used in place of the corresponding file names in conflict reports. That is, merge -L x -L y -L z a b c generates output that looks like it came from files x, y and z instead of from files a, b and c.
-p Send results to standard output instead of overwriting file1.
-q Quiet mode. Do not warn about conflicts.
-V Print RCS's version number.

merge examples

Let's say we have a file named orig.txt with the following contents.

Apples are red.
Oranges are orange.
Blueberries are delicious.

...and a file named mod1.txt, which is a modified version of orig.txt:

Apples are obviously red.
Oranges are blue.
Blueberries are delicious.

...and a file named mod2.txt, which is also a modified version of orig.txt:

Apples are obviously red.
Oranges are NOT blue.
Blueberries are delicious.

...and we run merge as follows:

merge mod1.txt orig.txt mod2.txt

It will analyze all three files, write to mod1.txt, and display the following warning:

merge: warning: conflicts during merge

This means that the merge was successful, but we should be aware that there was a conflict. If we open mod1.txt — which by default is the file where the merge is written — we will find that it now contains the following text:

<<<<<<< mod1.txt
Apples are obviously red.
Oranges are blue.
=======
Apples are obviously red.
Oranges are NOT blue.
>>>>>>> mod2.txt
Blueberries are delicious.

It is up to us to decide which "Oranges are..." line to keep (or to combine them in our own way), and make the edit to the file manually.

diff — Identify the differences between two files.