{"meta":{"title":"配置 Git 处理行结束符","intro":"为避免差异中出现问题，可配置 Git 正常处理行标题。","product":"开始","breadcrumbs":[{"href":"/zh/get-started","title":"开始"},{"href":"/zh/get-started/git-basics","title":"Git 基本知识"},{"href":"/zh/get-started/git-basics/configuring-git-to-handle-line-endings","title":"处理行结束"}],"documentType":"article"},"body":"# 配置 Git 处理行结束符\n\n为避免差异中出现问题，可配置 Git 正常处理行标题。\n\n## 关于行结尾\n\n每次按键盘上的 return 时，会插入一个称为行结束符的不可见字符<kbd></kbd>。 不同的操作系统处理行结束符的方式不同。\n\n当你使用 Git 和 GitHub 协作处理项目时，如果例如你在 Windows 计算机上工作，而你的协作者在 macOS 中进行了更改，Git 可能会产生意外结果。\n\n您可以将 Git 配置为自动处理行结束符，以便与使用不同操作系统的人员有效地协作。\n\n## 行结束符的全局设置\n\n`git config core.autocrlf` 命令用于更改 Git 处理行尾的方式。 它将采用单一参数。\n\n<div class=\"ghd-tool mac\">\n\n在 macOS 上，只需将 `input` 传递给配置即可。 例如：\n\n```shell\n$ git config --global core.autocrlf input\n# Configure Git to ensure line endings in files you checkout are correct for macOS\n```\n\n</div>\n\n<div class=\"ghd-tool windows\">\n\n在Windows上，只需将 `true` 传递给配置。 例如：\n\n```shell\n$ git config --global core.autocrlf true\n# Configure Git to ensure line endings in files you checkout are correct for Windows.\n# For compatibility, line endings are converted to Unix style when you commit files.\n```\n\n</div>\n\n<div class=\"ghd-tool linux\">\n\n在 Linux 上，只需将 `input` 传递给配置即可。 例如：\n\n```shell\n$ git config --global core.autocrlf input\n# Configure Git to ensure line endings in files you checkout are correct for Linux\n```\n\n</div>\n\n## 按仓库设置\n\n你可以选择配置 `.gitattributes` 文件来管理 Git 读取特定存储库中的行结束符的方式。 将此文件提交到存储库时，它将覆盖所有存储库贡献者的 `core.autocrlf` 设置。 这可确保所有用户的行为一致，而不管其 Git 设置和环境如何。\n\n`.gitattributes` 文件必须在存储库的根目录下创建，且像任何其他文件一样提交。\n\n`.gitattributes` 文件看上去像一个有两列的表格：\n\n* 左侧是 Git 要匹配的文件名。\n* 右侧是 Git 应对这些文件使用的行结束符配置。\n\n### 示例\n\n下面是一个示例 `.gitattributes` 文件。 您可以将其用作仓库的模板：\n\n```text\n# Set the default behavior, in case people don't have core.autocrlf set.\n* text=auto\n\n# Explicitly declare text files you want to always be normalized and converted\n# to native line endings on checkout.\n*.c text\n*.h text\n\n# Declare files that will always have CRLF line endings on checkout.\n*.sln text eol=crlf\n\n# Denote all files that are truly binary and should not be modified.\n*.png binary\n*.jpg binary\n```\n\n你会注意到，文件是匹配的，即 `*.c`、`*.sln`、`*.png`（用空格分隔），然后给定一个设置，即 `text`、`text eol=crlf`、`binary`。 我们将在下面介绍一些可能的设置。\n\n* `text=auto` Git 将以其认为的最佳方式处理文件。 这是一个合适的默认选项。\n\n*               `text eol=crlf` Git 始终在签出时将行尾转换为 `CRLF`。 你应将其用于必须保持 `CRLF` 结束符的文件，即使在 OSX 或 Linux 上。\n\n*               `text eol=lf` Git 始终在签出时将行尾转换为 `LF`。 你应将其用于必须保持 LF 结束符的文件，即使在 Windows 上。\n\n* `binary` Git 会理解指定文件不是文本，并且不应尝试更改这些文件。 该 `binary` 设置也是 `-text -diff` 的别名。\n\n## 在更改行结束符后刷新仓库\n\n设置 `core.autocrlf` 选项或提交 `.gitattributes` 文件后，Git 会自动更改行结束符以匹配新配置。 你可能会发现，Git 会报告你未修改的文件的更改。\n\n若要确保存储库中的所有行结束符都与新配置匹配，请使用 Git 备份文件，然后移除并还原所有文件以规范化行结束符。\n\n1. 在添加或提交任何更改之前，请验证 Git 是否已正确应用配置。 例如，Git 自动确定存储库中的文件是文本文件还是二进制文件。 为了避免存储库中的二进制文件损坏，建议在 `.gitattributes` 中将文件显式标记为二进制文件。 有关详细信息，请参阅 Git 文档中的 [gitattributes - 按路径定义属性](https://www.git-scm.com/docs/gitattributes#_marking_files_as_binary)。\n1. 若要避免丢失对存储库中的文件的任何本地更改，请运行以下命令添加并提交任何未完成的更改。\n\n   ```shell copy\n   git add . -u\n   git commit -m \"Saving files before refreshing line endings\"\n   ```\n\n1. 若要更新当前分支上的所有文件以反映新配置，请运行以下命令。\n\n   ```shell copy\n   git add --renormalize .\n   ```\n\n1. 若要显示重写的规范化文件，请运行以下命令。\n\n   ```shell copy\n   git status\n   ```\n\n1. （可选）若要在存储库中提交任何未完成的更改，请运行以下命令。\n\n   ```shell copy\n   git commit -m \"Normalize all the line endings\"\n   ```\n\n## 延伸阅读\n\n* Pro Git 书籍中的[自定义 Git - Git 属性](https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes)\n* Git 的手册页中的 [git-config](https://git-scm.com/docs/git-config)\n* Pro Git 书籍中的[快速入门：初次 Git 设置](https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup)\n* [注意行结尾](http://adaptivepatchwork.com/2012/03/01/mind-the-end-of-your-line/) 由 [Tim Clem](https://github.com/tclem) 编写"}