Git 处理换行符的配置方法

Posted cbhe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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. This is because Windows uses both a carriage-return character and a linefeed character for newlines in its files, whereas macOS and Linux systems use only the linefeed character. 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.

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. You can turn on this functionality with the core.autocrlf setting. If you’re on a Windows machine, set it to true?—?this converts LF endings into CRLF when you check out code:

$ git config --global core.autocrlf true

如果团队中有人用mac有人用win则推荐用win的人把core.autocrlf设置为true

If you’re on a Linux or macOS system that uses LF line endings, then you don’t want Git to automatically convert them when you check out files; however, if a file with CRLF endings accidentally gets introduced, then you may want Git to fix it. You can tell Git to convert CRLF to LF on commit but not the other way around by setting core.autocrlf to input:

$ git config --global core.autocrlf input

团队中用mac或者用linux的程序员如果偶尔会遇到以CRLF为行结尾的文件时,可以将core.autocrlf设置为input,这样在push的时候讲CRLF转换成LF,且pull的时候不会进行任何转换

This setup should leave you with CRLF endings in Windows checkouts, but LF endings on macOS and Linux systems and in the repository.

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:

$ git config --global core.autocrlf false

如果团队中所有人都用win并且也想在git仓库中保持CRLF的格式,则设置为false

以上是关于Git 处理换行符的配置方法的主要内容,如果未能解决你的问题,请参考以下文章

git换行符

有关git的换行符的处理问题

Git 2.26.1修复使用换行符创建可能的凭据泄漏

git LF 和 CRLF换行的问题

git LF 和 CRLF换行的问题

Windows与Linux行尾换行符引发Git的一系列惨案