It's very easy for patches or other collaborated work to introduce subtle whitespace changes because editors silently introduce them, and if your files ever touch a Windows system, their line endings might be replaced. (补丁程序或其他协作工作很容易引入细微的空格变化,因为编辑者会默默地引入它们,并且如果您的文件曾经接触过Windows系统,则它们的行尾可能会被替换。)
Git has a few configuration options to help with these issues. (Git有一些配置选项可帮助解决这些问题。)
core.autocrlf
If you're programming on Windows and working with people who are not (or vice-versa), you'll probably run into line-ending issues at some point.
(如果您在Windows上进行编程,并且与非Windows的人一起工作(反之亦然),则有时可能会遇到行尾问题。)
This is because Windows uses both a carriage-return character and a linefeed character for newlines in its files, whereas Mac and Linux systems use only the linefeed character. (这是因为Windows在其文件中的换行符中使用了回车符和换行符,而Mac和Linux系统仅使用了换行符。)
This is a subtle but incredibly annoying fact of cross-platform work; (这是跨平台工作的一个微妙但令人讨厌的事实。)
many editors on Windows silently replace existing LF-style line endings with CRLF, or insert both line-ending characters when the user hits the enter key. (Windows上的许多编辑器都以CRLF静默替换现有的LF样式的行尾,或者在用户按下Enter键时插入两个行尾字符。)
Git can handle this by auto-converting CRLF line endings into LF when you add a file to the index, and vice versa when it checks out code onto your filesystem.
(当您将文件添加到索引时,Git可以通过将CRLF行尾自动转换为LF来解决此问题,反之亦然,当它将代码签出到文件系统中时,反之亦然。)
You can turn on this functionality with the core.autocrlf setting. (您可以使用core.autocrlf设置启用此功能。)
If you're on a Windows machine, set it to true – this converts LF endings into CRLF when you check out code: (如果您使用的是Windows计算机,请将其设置为true –在签出代码时,这会将LF结尾转换为CRLF:)
$ git config --global core.autocrlf true
If you're on a Linux or Mac system that uses LF line endings, then you don't want Git to automatically convert them when you check out files;
(如果您使用的是使用LF行尾的Linux或Mac系统,那么您不希望Git在签出文件时自动将它们转换;)
however, if a file with CRLF endings accidentally gets introduced, then you may want Git to fix it. (但是,如果意外引入了带有CRLF结尾的文件,则您可能需要Git对其进行修复。)
You can tell Git to convert CRLF to LF on commit but not the other way around by setting core.autocrlf to input: (您可以通过将core.autocrlf设置为input来告诉Git在提交时将CRLF转换为LF,反之则不行:)
$ git config --global core.autocrlf input
This setup should leave you with CRLF endings in Windows checkouts, but LF endings on Mac and Linux systems and in the repository.
(此设置应使您在Windows检出中具有CRLF结尾,但在Mac和Linux系统以及存储库中具有LF结尾。)
If you're a Windows programmer doing a Windows-only project, then you can turn off this functionality, recording the carriage returns in the repository by setting the config value to false:
(如果您是Windows程序员,并且仅在Windows上执行项目,则可以关闭此功能,通过将config值设置为false来在存储库中记录回车:)
$ git config --global core.autocrlf false