> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man1/diff3.1.md).

# diff3.1

`diff3` — 三方文件差异比较

## 名称

`diff3`

## 概要

`diff3 [-3AaEeimTXx] [--diff-program program] [--strip-trailing-cr] [-L label1 | --label label1] [-L label2 | --label label2] [-L label3 | --label label3] file1 file2 file3`

`diff3 [--help] [--version]`

## 描述

`diff3` 实用程序比较一个文件的三个不同版本 `file1`、`file2` 和 `file3` 的内容，并将结果写入标准输出。各选项描述了将不同版本合并和清理为新文件的不同方法。rcs(1) 使用 `diff3` 来合并特定版本或创建新版本。

选项如下：

**`-3`**, **`--easy-only`** 生成适合 ed(1) 的输出脚本，仅包含特定于 `file3` 的更改。

**`-A`**, **`--show-all`** 输出所有更改，冲突部分用括号标注。

**`-a`**, **`--text`** 将所有文件视为 ASCII 文本。

**`-E`**, **`--show-overlap`**

**`-X`** 分别类似于 `-e` 和 `-x`，但对重叠更改（即在正常列表中以 `====` 标注的更改）的处理方式不同。编辑脚本会插入两个文件中的重叠行，并以“`<<<<<<`”和“`>>>>>>`”行括起。

**`-e`**, **`--ed`** 生成适合作为 ed(1) 实用程序输入脚本的输出形式。该脚本随后可用于合并三个文件之间的共同差异以及特定于 `file1` 和 `file3` 的差异。换言之，`-e` 选项忽略特定于 `file1` 和 `file2` 的差异，以及特定于 `file2` 和 `file3` 的差异。它适用于撤销仅特定于 `file2` 的更改。

**`--help`** 打印用法信息并退出。

**`-i`** 追加 `w` 和 `q` ed(1) 命令。

**`-L`**, **`--label`** 定义要打印的标签，以替代文件名 `file1`、`file2` 和 `file3`。

**`-m`**, **`--merge`** 合并输出，而非生成 ed 脚本。

**`-T`**, **`--initial-tab`** 在正常列表中，每行开头使用制表符代替两个空格。在生成 ed(1) 脚本的模式下，此选项无影响。

**`-x`**, **`--overlap-only`** 生成适合 ed(1) 的输出脚本，仅包含特定于所有三个版本的更改。

**`--diff-program`** `program` 使用 `program` 代替默认的 [diff(1)](/man/man1/diff.1.md) 来比较文件。

**`--strip-trailing-cr`** 去除输入文件上的尾部回车符。

**`--version`** 打印版本信息并退出。

`-E` 选项被 RCS merge(1) 使用，以确保合并文件中的重叠更改被保留并引起注意。

例如，假设 `file1` 和 `file2` 中的第 7-8 行都被更改。将以下命令生成的编辑脚本

```sh
$ diff3 -E file1 file2 file3
```

应用到 `file1` 会得到如下文件：

```sh
lines 1-6
of file1
<<<<<<< file1
lines 7-8
of file1
=======
lines 7-8
of file3
>>>>>>> file3
rest of file1
```

`diff3` 的默认输出标注所有文件之间的差异，以及每对文件之间特定的差异。更改通过 ed(1) 从不同版本创建所需目标所需的命令来描述。有关命令的描述，请参见 [diff(1)](/man/man1/diff.1.md)。

**`====`** 此标注下方的行是所有文件之间不同的行范围。

**`====`**`n` 此标注下方的行是文件 `n` 中独有的不同行范围。

## 退出状态

`diff3` 实用程序成功时退出状态为 0，发生错误时退出状态 >1。此外，如果指定了 `-A`、`-E`、`-L`、`-m` 或 `-X` 标志，发现冲突时退出状态为 1。

## 参见

[diff(1)](/man/man1/diff.1.md), ed(1), merge(1), rcs(1), [sdiff(1)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man1/sdiff.1.md)

## 历史

`diff3` 命令首次出现于 Version 7 AT\&T UNIX。

## 缺陷

`-e` 选项无法捕获和更改以 `.` 作为行首且为行中唯一字符的行。生成的脚本会在该行失败，因为 `.` 是 ed(1) 的编辑命令。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man1/diff3.1.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
