5+ Practical “merge” Command Usage Examples in Linux

«
»

This tutorial explains Linux “merge” command, options and its usage with examples.

“merge” command is used for Three-way file merge. This post describes “merge” command used in Linux along with usage examples and/or output.

Usage:
merge [ options ] file1 file2 file3

advertisement

merge incorporates all changes that lead from file2 to file3 into file1. The result ordinarily goes into file1. merge is useful for combining separate changes to an original. Suppose file2 is the original, and both file1 and file3 are modifications of file2. Then merge combines both changes.

Note
* merge is a very rarely used command.
* When merging files all files must exist.

Here’s the listing of example usage of “merge” command:

To install merge type in terminal .

advertisement
advertisement
sudo apt-get install rcs

1. To show conflicts between the files:

sanfoundry-> touch file1 file2 file3
sanfoundry-> cat file2 
Hi i am a nice boy
 
I like cricket
sanfoundry-> cat file3
Hi i am a boy 
 
I like football
sanfoundry-> merge file1 file2 file3 
merge: warning: conflicts during merge
sanfoundry-> cat file1 
<<<<<<< file1
=======
Hi i am a boy 
 
I like football
>>>>>>> file3

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.

advertisement

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

2. For a verbose output(merge -A file1 file2 file3):
Output conflicts using the -A style of diff3, if supported by diff3. This merges all changes leading from file2 to file3 into file1, and generates the most verbose output.

sanfoundry-> merge -A file1 file2 file3 
merge: warning: conflicts during merge
sanfoundry-> cat file1 
<<<<<<< file1
||||||| file2
Hi i am a nice boy
 
I like cricket
=======
Hi i am a boy 
 
I like football
>>>>>>> file3

3. For lesser detail output than verbose(merge -E file1 file2 file3):
These options specify conflict styles that generate less information than -A.

sanfoundry-> merge -E file1 file2 file3 
merge: warning: conflicts during merge
sanfoundry-> cat file1 
<<<<<<< file1
=======
Hi i am a boy 
 
I like football
>>>>>>> file3

4. To send results to standard output instead of overwriting file1(merge -e file1 file2 file3):

advertisement
sanfoundry-> merge -p file1 file2 file3 
<<<<<<< file1
=======
Hi i am a boy 
 
I like football
>>>>>>> file3
merge: warning: conflicts during merge

5. To be quiet and do not warn about conflicts(merge -q file1 file2 file3):

sanfoundry-> merge -q file1 file2 file3

To Print ‘s version number:

advertisement
sanfoundry-> merge -V file1 file2 file3 
RCS version 5.7

6. To lable files(merge -L 1 -L 2 -L 3 file1 file2 file3):
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.

sanfoundry-> merge -L 1 -L 2 -L 3 file1 file2 file3 
merge: warning: conflicts during merge
sanfoundry-> cat file1 
<<<<<<< 1
=======
Hi i am a boy 
 
I like football
>>>>>>> 3

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter