# 配置 Git 处理行结束符

为避免差异中出现问题，可配置 Git 正常处理行标题。

## 关于行结尾

每次按键盘上的 return 时，会插入一个称为行结束符的不可见字符<kbd></kbd>。 不同的操作系统处理行结束符的方式不同。

当你使用 Git 和 GitHub 协作处理项目时，如果例如你在 Windows 计算机上工作，而你的协作者在 macOS 中进行了更改，Git 可能会产生意外结果。

您可以将 Git 配置为自动处理行结束符，以便与使用不同操作系统的人员有效地协作。

## 行结束符的全局设置

`git config core.autocrlf` 命令用于更改 Git 处理行尾的方式。 它将采用单一参数。

<div class="ghd-tool mac">

在 macOS 上，只需将 `input` 传递给配置即可。 例如：

```shell
$ git config --global core.autocrlf input
# Configure Git to ensure line endings in files you checkout are correct for macOS
```

</div>

<div class="ghd-tool windows">

在Windows上，只需将 `true` 传递给配置。 例如：

```shell
$ git config --global core.autocrlf true
# Configure Git to ensure line endings in files you checkout are correct for Windows.
# For compatibility, line endings are converted to Unix style when you commit files.
```

</div>

<div class="ghd-tool linux">

在 Linux 上，只需将 `input` 传递给配置即可。 例如：

```shell
$ git config --global core.autocrlf input
# Configure Git to ensure line endings in files you checkout are correct for Linux
```

</div>

## 按仓库设置

你可以选择配置 `.gitattributes` 文件来管理 Git 读取特定存储库中的行结束符的方式。 将此文件提交到存储库时，它将覆盖所有存储库贡献者的 `core.autocrlf` 设置。 这可确保所有用户的行为一致，而不管其 Git 设置和环境如何。

`.gitattributes` 文件必须在存储库的根目录下创建，且像任何其他文件一样提交。

`.gitattributes` 文件看上去像一个有两列的表格：

* 左侧是 Git 要匹配的文件名。
* 右侧是 Git 应对这些文件使用的行结束符配置。

### 示例

下面是一个示例 `.gitattributes` 文件。 您可以将其用作仓库的模板：

```text
# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
```

你会注意到，文件是匹配的，即 `*.c`、`*.sln`、`*.png`（用空格分隔），然后给定一个设置，即 `text`、`text eol=crlf`、`binary`。 我们将在下面介绍一些可能的设置。

* `text=auto` Git 将以其认为的最佳方式处理文件。 这是一个合适的默认选项。

*               `text eol=crlf` Git 始终在签出时将行尾转换为 `CRLF`。 你应将其用于必须保持 `CRLF` 结束符的文件，即使在 OSX 或 Linux 上。

*               `text eol=lf` Git 始终在签出时将行尾转换为 `LF`。 你应将其用于必须保持 LF 结束符的文件，即使在 Windows 上。

* `binary` Git 会理解指定文件不是文本，并且不应尝试更改这些文件。 该 `binary` 设置也是 `-text -diff` 的别名。

## 在更改行结束符后刷新仓库

设置 `core.autocrlf` 选项或提交 `.gitattributes` 文件后，Git 会自动更改行结束符以匹配新配置。 你可能会发现，Git 会报告你未修改的文件的更改。

若要确保存储库中的所有行结束符都与新配置匹配，请使用 Git 备份文件，然后移除并还原所有文件以规范化行结束符。

1. 在添加或提交任何更改之前，请验证 Git 是否已正确应用配置。 例如，Git 自动确定存储库中的文件是文本文件还是二进制文件。 为了避免存储库中的二进制文件损坏，建议在 `.gitattributes` 中将文件显式标记为二进制文件。 有关详细信息，请参阅 Git 文档中的 [gitattributes - 按路径定义属性](https://www.git-scm.com/docs/gitattributes#_marking_files_as_binary)。
1. 若要避免丢失对存储库中的文件的任何本地更改，请运行以下命令添加并提交任何未完成的更改。

   ```shell copy
   git add . -u
   git commit -m "Saving files before refreshing line endings"
   ```

1. 若要更新当前分支上的所有文件以反映新配置，请运行以下命令。

   ```shell copy
   git add --renormalize .
   ```

1. 若要显示重写的规范化文件，请运行以下命令。

   ```shell copy
   git status
   ```

1. （可选）若要在存储库中提交任何未完成的更改，请运行以下命令。

   ```shell copy
   git commit -m "Normalize all the line endings"
   ```

## 延伸阅读

* Pro Git 书籍中的[自定义 Git - Git 属性](https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes)
* Git 的手册页中的 [git-config](https://git-scm.com/docs/git-config)
* Pro Git 书籍中的[快速入门：初次 Git 设置](https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup)
* [注意行结尾](http://adaptivepatchwork.com/2012/03/01/mind-the-end-of-your-line/) 由 [Tim Clem](https://github.com/tclem) 编写