Git,将文件添加到存储库会导致 LF ->CRLF 出现致命错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8651708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git, adding files to repository gives fatal error for LF ->CRLF
提问by user619656
I'm new to git and I need some help. I'm using msysgit on windows.
我是 git 新手,我需要一些帮助。我在 Windows 上使用 msysgit。
When I execute the command git add [folderName]
I get the response:
当我执行命令时,git add [folderName]
我得到响应:
fatal: LF would be replaced by CRLF in [.css file or .js file]
and then if you try to do a commit nothing happens.
然后如果你尝试提交什么都不会发生。
$ git commit
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# so01/
nothing added to commit but untracked files present (use "git add" to track)
Some of these css/js files were downloaded from the net so I guess that's why the have LF. If I open the file and cut/paste the content, then I get the error on the next file and so on.
其中一些 css/js 文件是从网上下载的,所以我想这就是为什么有 LF。如果我打开文件并剪切/粘贴内容,那么我会在下一个文件中收到错误,依此类推。
Any help will be much appreciated.
任何帮助都感激不尽。
Edit
编辑
Setting core.autocrlf
to false seems to solve the problem, but I read on many posts not to set this option to false.
设置core.autocrlf
为 false 似乎可以解决问题,但我在很多帖子中都没有将此选项设置为 false。
Can somebody point me where can I find out what problems may arise in this situation?
有人可以指出我在哪里可以找出在这种情况下可能出现的问题吗?
采纳答案by Adam Dymitruk
Trust the code editors to manipulate your line endings. Auto crlf should be false. Don't let source control get too smart. If don't need to have your source control tool to change your line endings, don't. This will hurt.
相信代码编辑器可以操作您的行尾。自动 crlf 应该是假的。不要让源代码控制变得太聪明。如果不需要使用源代码控制工具来更改行尾,请不要使用。这会很痛。
To reiterate from an accepted answer: "Unless you can see specific treatment which must deal with native eol, you are better off leaving autocrlf to false."
重申一个已接受的答案:“除非您可以看到必须处理本地 eol 的特定处理,否则最好将 autocrlf 设置为 false。”
Also from the progit book at the end of the section on autocrlf:
同样来自 autocrlf 部分末尾的 progit 书:
"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 的项目,那么你可以关闭这个功能,通过将配置值设置为 false 来在存储库中记录回车”
The only other help I can give is that if you take the other route, get familiar with vim -b
which will show special characters such as the CR in MSysGit, and git show HEAD:path/to/your/file.txt
which shouldshow you the file in the way that git stored it.
我可以提供的唯一其他帮助是,如果您采用另一条路线,请熟悉vim -b
哪些将显示特殊字符,例如 MSysGit 中的 CR,以及git show HEAD:path/to/your/file.txt
哪些应该以 git 存储文件的方式显示文件。
Set core.whitespace cr-at-eol
to have patches and diffs not highlight CRs as possible problematic whitespace.
设置core.whitespace cr-at-eol
补丁和差异不突出 CR 作为可能有问题的空白。
Not worth the hassle. Store as-is.
不值得麻烦。按原样存储。
回答by sigmapi13
very new to this so setting core.autocrlf to false didn't make too much sense to me. So for other newbies, go to the config file in you .git folder and add:
对此非常陌生,因此将 core.autocrlf 设置为 false 对我来说没有太大意义。因此,对于其他新手,请转到 .git 文件夹中的配置文件并添加:
[core]
autocrlf = false
under the [core] heading.
在[核心]标题下。
回答by m0tive
The problem is probably occurring because you set Git to store files internally with crlf
with the core.eol
setting. When you add a file, Git is warning you it will change it to the internal format.
出现问题的原因可能是您将 Git 设置为crlf
使用该core.eol
设置在内部存储文件。当您添加文件时,Git 会警告您将其更改为内部格式。
Git works best with lf
line endings, so if possible always work with core.eol = lf
.
Git 最适用于lf
行尾,因此如果可能,请始终使用core.eol = lf
.
This should explain when to use core.autocrlf
, Why should I use core.autocrlf=true in Git?
这应该解释什么时候使用core.autocrlf
,为什么我应该在 Git 中使用 core.autocrlf=true?
You may also want to use core.safecrlf
. Check git config --help
for details on the settings.
您可能还想使用core.safecrlf
. 检查git config --help
有关设置的详细信息。
回答by koppor
The git autodetection of formats works pretty well. Therefore, core.autocrlf=true
is a really good idea on Windows.
git 自动检测格式效果很好。因此,core.autocrlf=true
在 Windows 上是一个非常好的主意。
Saying git config --global core.safecrlf=false
says to git: Hey, please convert my wrongline endings (LF only) to Windows line endings (CRLF) and do not bother me with it.
话说git config --global core.safecrlf=false
说与git:嘿,请转换我错了行结束(LF只)到Windows行尾(CRLF),不要打扰它了我。
Therefore, you should really disable core.safecrlf
.
因此,您应该真正禁用core.safecrlf
.
Longer answer at: https://stackoverflow.com/a/15471083/873282