git 用 CRLF 替换 LF
在 Windows XP 机器上运行 git,使用 bash.我从 SVN 导出了我的项目,然后克隆了一个裸仓库.
Running git on a Windows XP machine, using bash. I exported my project from SVN, and then cloned a bare repository.
然后我将导出粘贴到裸存储库目录中,并做了一个:
I then pasted the export into the bare repositories directory, and did a:
git add -A
然后我收到了一个消息列表:
I then got a list of messages saying:
LF 将被 CRLF 替换
LF will be replaced by CRLF
这种转换的后果是什么?这是 Visual Studio 中的 .NET 解决方案.
What are the ramifications of this conversion? This is a .NET solution in Visual Studio.
这些消息是由于 Windows 上 core.autocrlf
的默认值不正确造成的.
These messages are due to incorrect default value of core.autocrlf
on Windows.
autocrlf
的概念是透明地处理行尾转换.确实如此!
The concept of autocrlf
is to handle line endings conversions transparently. And it does!
坏消息:需要手动配置值.
好消息:每个 git 安装应该只执行一次(每个项目设置也是可能的).
Bad news: value needs to be configured manually.
Good news: it should only be done ONE time per git installation (per project setting is also possible).
autocrlf
的工作原理:
How autocrlf
works:
core.autocrlf=true: core.autocrlf=input: core.autocrlf=false:
repo repo repo
^ V ^ V ^ V
/ / /
crlf->lf lf->crlf crlf->lf /
/ / /
这里 crlf
= win 风格的行尾标记,lf
= unix 风格(和 mac osx).
Here crlf
= win-style end-of-line marker, lf
= unix-style (and mac osx).
(pre-osx cr
不影响上述三个选项中的任何一个)
(pre-osx cr
in not affected for any of three options above)
这个警告什么时候出现(在 Windows 下)
– autocrlf
= true
如果您的文件之一(= 很少)中有 unix 样式的 lf
,
– autocrlf
= input
如果你的一个文件中有 win 风格的 crlf
(= 几乎总是),
– autocrlf
= false
– 从不!
– autocrlf
= true
if you have unix-style lf
in one of your files (= RARELY),
– autocrlf
= input
if you have win-style crlf
in one of your files (= almost ALWAYS),
– autocrlf
= false
– NEVER!
这个警告是什么意思
警告LF 将替换为 CRLF";说你(有 autocrlf
=true
)将在提交结账周期后丢失你的 unix 风格的 LF(它将被 windows 风格的 CRLF 取代).Git 不希望你在 windows 下使用 unix 风格的 LF.
The warning "LF will be replaced by CRLF" says that you (having autocrlf
=true
) will lose your unix-style LF after commit-checkout cycle (it will be replaced by windows-style CRLF). Git doesn't expect you to use unix-style LF under windows.
警告CRLF 将被 LF 替换";说你(有 autocrlf
=input
)在提交-结账周期后将丢失你的 windows 风格的 CRLF(它将被 unix 风格的 LF 取代).windows下不要使用input
.
The warning "CRLF will be replaced by LF" says that you (having autocrlf
=input
) will lose your windows-style CRLF after a commit-checkout cycle (it will be replaced by unix-style LF). Don't use input
under windows.
另一种展示autocrlf
如何工作的方式
Yet another way to show how autocrlf
works
1) true: x -> LF -> CRLF
2) input: x -> LF -> LF
3) false: x -> x -> x
其中 x 是 CRLF(windows 风格)或 LF(unix 风格),箭头代表
where x is either CRLF (windows-style) or LF (unix-style) and arrows stand for
file to commit -> repository -> checked out file
如何修复
core.autocrlf
的默认值在 git 安装过程中被选择并存储在系统范围的 gitconfig (%ProgramFiles(x86)%gitetcgitconfig
on windows, /etc/gitconfig
在 Linux 上).还有(按以下顺序级联):
Default value for core.autocrlf
is selected during git installation and stored in system-wide gitconfig (%ProgramFiles(x86)%gitetcgitconfig
on windows, /etc/gitconfig
on linux). Also there're (cascading in the following order):
– 全局"(per-user) gitconfig 位于 ~/.gitconfig
,另一个
– 全球"(每个用户)位于 $XDG_CONFIG_HOME/git/config
或 $HOME/.config/git/config
和
的 gitconfig– 本地"(per-repo) gitconfig at .git/config
在工作目录中.
– "global" (per-user) gitconfig located at ~/.gitconfig
, yet another
– "global" (per-user) gitconfig at $XDG_CONFIG_HOME/git/config
or $HOME/.config/git/config
and
– "local" (per-repo) gitconfig at .git/config
in the working dir.
因此,在工作目录中写入git config core.autocrlf
以检查当前使用的值和
So, write git config core.autocrlf
in the working dir to check the currently used value and
– git config --system core.autocrlf false
# 每个系统的解决方案
– git config --global core.autocrlf false
# 每个用户的解决方案
– git config --local core.autocrlf false
# 每个项目的解决方案
– git config --system core.autocrlf false
# per-system solution
– git config --global core.autocrlf false
# per-user solution
– git config --local core.autocrlf false
# per-project solution
警告
– git config
设置可以被 gitattributes
设置覆盖.
– crlf ->lf 转换仅在添加新文件时发生,已存在于 repo 中的 crlf
文件不受影响.
Warnings
– git config
settings can be overridden by gitattributes
settings.
– crlf -> lf
conversion only happens when adding new files, crlf
files already existing in the repo aren't affected.
道德(适用于 Windows):
- 使用 core.autocrlf
= true
如果您也打算在 Unix 下使用这个项目(并且不愿意将您的编辑器/IDE 配置为使用 unix 行尾),
- 使用 core.autocrlf
= false
如果您打算仅在 Windows 下使用此项目(或者您已将编辑器/IDE 配置为使用 unix 行尾),
- 从不使用 core.autocrlf
= input
除非你有充分的理由(例如,如果你正在使用Windows 下的 unix 实用程序,或者如果您遇到 makefile 问题),
Moral (for Windows):
- use core.autocrlf
= true
if you plan to use this project under Unix as well (and unwilling to configure your editor/IDE to use unix line endings),
- use core.autocrlf
= false
if you plan to use this project under Windows only (or you have configured your editor/IDE to use unix line endings),
- never use core.autocrlf
= input
unless you have a good reason to (eg if you're using unix utilities under windows or if you run into makefiles issues),
PS 安装 git for Windows 时选择什么?
如果您不打算在 Unix 下使用您的任何项目,不要同意默认的第一个选项.选择第三个(按原样结帐,按原样提交).您不会看到此消息.曾经.
PS What to choose when installing git for Windows?
If you're not going to use any of your projects under Unix, don't agree with the default first option. Choose the third one (Checkout as-is, commit as-is). You won't see this message. Ever.
PPS 我的个人偏好是将 editor/IDE 配置为使用 Unix 风格的结尾,并将 core.autocrlf
设置为 false
.
PPS My personal preference is configuring the editor/IDE to use Unix-style endings, and setting core.autocrlf
to false
.