命令行大全

merge

rose1 发表于 2020-10-08 05:17浏览次数:

在类似Unix的操作系统上,merge命令执行三向文件合并。 合并过程将分析三个文件:一个基本版本和两个冲突的修改版本。它尝试基于共享的基本版本将两组修改自动组合到一个合并的文件中。如果无法进行自动合并,则将有助于手动合并。 本文档介绍了GNU / Linux版本的合并。

查看英文版

目录:

1 merge 运行系统环境

2 merge 说明

3 merge的工作方式

4 merge 语法

5 merge 例子

merge 运行系统环境

Linux

merge 说明

merge是RCS版本控制程序包的一部分。它用于执行三向文件合并。

merge分析三个文件-一个原始文件和两个原始版本的修改版本-并逐行比较它们,以尝试解决两组修改之间的差异,以创建一个表示两个文件的统一文件套变更。

根据两组更改之间的差异,这可能是自动过程,也可能需要用户输入。

如果两组变更中的任何一组都不冲突,则merge通常可以自行确定要做什么。但是,如果两组更改发生冲突(例如,如果两个修改的文件中同一行文字的措词不同),则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 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.

查看英文版

查看中文版

merge的工作方式

merge将所有从file2到file3的更改合并到file1中。结果通常进入file1。

假设file2是原始文件,而file1和file3都是对file2的修改。然后合并将两个更改合并在一起。

如果file1和file3在同一行的行中都有更改,则会发生冲突。如果发现冲突,则merge通常会输出警告,并用“ <<<<<<< ”“ >>>>>>> ”行将冲突括起来。例如,典型的冲突如下所示:

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

如果存在冲突,则用户应编辑结果并删除替代方法之一。

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 语法

merge [options] file1 file2 file3

选件

-A 使用diff3(1)的-A样式(如果diff3支持),则输出冲突。这会将从file2到file3的所有更改合并到file1中,并生成最详细的输出。
-E-e 这些选项指定产生的信息少于-A的冲突样式。有关详细信息,请参见diff3(1)。缺省值为-E。使用-e,merge不会警告冲突。
-L 标签 该选项最多可给出3次,并指定用于代替冲突报告中相应文件名的标签。也就是说,merge -L x -L y -L z a b c生成的输出看起来像是来自文件x,yz而不是来自文件a,bc。
-p 将结果发送到标准输出,而不是覆盖 file1。
-q 静音模式。不要警告冲突。
-V 打印RCS的版本号。
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 xy and z instead of from files ab 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 例子

假设我们有一个名为orig.txt的文件,其中包含以下内容。

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

...并命名一个文件mod1.txt,这是修改后的版本orig.txt:

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

...以及一个名为mod2.txt的文件,该文件也是orig.txt的修改版本:

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

...然后运行合并,如下所示:

merge mod1.txt orig.txt mod2.txt

它将分析所有三个文件,写入mod1.txt,并显示以下警告:

merge: warning: conflicts during merge

这意味着合并成功,但是我们应该意识到存在冲突。如果打开mod1.txt(默认情况下是写入合并的文件),则会发现它现在包含以下文本:

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

由我们决定保留哪个“ Oranges are ... ”行(或以我们自己的方式组合它们),然后手动对文件进行编辑。

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.

查看英文版

查看中文版