文章版本: Enterprise Server 2.17
配置 Git 处理行结束符
为避免差异中出现问题,可配置 Git 正常处理行标题。
Every time you press return on your keyboard you insert an invisible character called a line ending. Different operating systems handle line endings differently.
When you're collaborating on projects with Git and GitHub Enterprise, Git might produce unexpected results if, for example, you're working on a Windows machine, and your collaborator has made a change in OS X.
You can configure Git to handle line endings automatically so you can collaborate effectively with people who use different operating systems.
行结束符的全局设置
git config core.autocrlf
命令用于更改 Git 处理行结束符的方式。 它将采用单一参数。
在 OS X 上,只需将 input(输入)
传递给配置。 例如:
$ git config --global core.autocrlf input
# Configure Git to ensure line endings in files you checkout are correct for OS X
在 Windows 上,只需将 true(真)
传递给配置。 例如:
$ 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.
在 Linux 上,只需将 input(输入)
传递给配置。 例如:
$ git config --global core.autocrlf input
# Configure Git to ensure line endings in files you checkout are correct for Linux
按仓库设置
Optionally, you can configure a .gitattributes file to manage how Git reads line endings in a specific repository. When you commit this file to a repository, it overrides the core.autocrlf
setting for all repository contributors. This ensures consistent behavior for all users, regardless of their Git settings and environment.
.gitattributes 文件必须在仓库的根目录下创建,且像任何其他文件一样提交。
.gitattributes 文件看上去像一个两列表格。
- 左侧是 Git 要匹配的文件名。
- 右侧是 Git 应对这些文件使用的行结束符配置。
示例
以下是 .gitattributes 文件示例。 You can use it as a template for your repositories:
# 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
You'll notice that files are matched—*.c
, *.sln
, *.png
—, separated by a space, then given a setting—text
, text eol=crlf
, binary
. We'll go over some possible settings below.
-
text=auto
Git will handle the files in whatever way it thinks is best. 这是一个合适的默认选项。 -
text eol=crlf
Git will always convert line endings toCRLF
on checkout. 您应将其用于必须保持CRLF
结束符的文件,即使在 OSX 或 Linux 上。 -
text eol=lf
Git will always convert line endings toLF
on checkout. 您应将其用于必须保持 LF 结束符的文件,即使在 Windows 上。 -
binary
Git will understand that the files specified are not text, and it should not try to change them.binary
设置也是-text -diff
的一个别名。
在更改行结束符后刷新仓库
When you set the core.autocrlf
option or commit a .gitattributes file, you may find that Git reports changes to files that you have not modified. Git has changed line endings to match your new configuration.
To ensure that all the line endings in your repository match your new configuration, backup your files with Git, delete all files in your repository (except the .git
directory), then restore the files all at once.
- 在 Git 中保存当前文件,以便不会丢失任何工作。
$ git add . -u $ git commit -m "Saving files before refreshing line endings"
- 添加回所有已更改的文件,然后标准化行结束符。
$ git add --renormalize .
- 显示已重写的标准化文件。
$ git status
- 将更改提交到仓库。
$ git commit -m "Normalize all the line endings"
延伸阅读
- Customizing Git - Git Attributes in the Pro Git book
- git-config in the man pages for Git
- Getting Started - First-Time Git Setup in the Pro Git book
- Mind the End of Your Line by Tim Clem