[git]git replacing LF with CRLF/git config --global core.autocrlf false

yejing@BENBENYE /F/git/shrn/SHRN.WebUI/views/shared (develop)
$ git add _Index.cshtml

fatal: LF would be replaced by CRLF in SHRN.WebUI/views/shared/_Index.cshtml

//fatal严重的错误

遇到这个错误,然后执行

$ git config --global core.autocrlf false

之后再添加文件或者commit都不会有之前的严重警告导致不能提交。

以下是对$ git config --global core.autocrlf false为什么要执行这句话做的解释(http://stackoverflow.com/questions/1967370/git-replacing-lf-with-crlf

/************* 引用start *****************/

Git has two modes of how it treats line endings:

$ git config core.autocrlf
# that command will print "true" or "false" or "input"

You can set the mode to use by adding an additional parameter of true or false to the above command line.

If core.autocrlf is set to true, that means that any time you add a file to the git repo that git thinks is a text file, it will turn all CRLF line endings to just LF before it stores it in the commit. Whenever you git checkout something, all text files automatically will have their LF line endings converted to CRLF endings. This allows development of a project across platforms that use different line-ending styles without commits being very noisy because each editor changes the line ending style as the line ending style is always consistently LF.

The side-effect of this convenient conversion, and this is what the warning you're seeing is about, is that if a text file you authored originally had LF endings instead of CRLF, it will be stored with LF as usual, but when checked out later it will have CRLF endings. For normal text files this is usually just fine. The warning is a "for your information" in this case, but in case git incorrectly assesses a binary file to be a text file, it is an important warning because git would then be corrupting your binary file.

If core.autocrlf is set to false, no line-ending conversion is ever performed, so text files are checked in as-is. This usually works ok, as long as all your developers are either on Linux or all on Windows. But in my experience I still tend to get text files with mixed line endings that end up causing problems.

My personal preference is to leave the setting turned ON, as a Windows developer.

See http://kernel.org/pub/software/scm/git/docs/git-config.html for updated info that includes the "input" value.

/************* 引用end *****************/
/************* 翻译start *****************/

如果 core.autocrlf 被设置为true,这就意味着你任何时候添加到git仓库的文件git都会认为它是一个text文件,他会在存储提交之前把所有的CRLF结尾都转换成LF。无论什么时候你 git checkout ,所有的文件的LF(换行)都会修改为CRLF(回车换行)。这样就允许项目跨平台开发,即便有不一致的行尾结束风格也都会保证在 commit之前都一致转换为LF(换行)
用这个方便的转换方式的副作用,就是像你刚才看到的会出现那种警告,就是如果你最初创建的一个文件用LF()代替了CRLF,那将会按照平常一样用LF存储,但是当checkout 的时候就会出现CRLF,对于一些正正常的文本文件,这些还好。但是万一git 错误的评估了一个二进制文件时,这会是一个很重要的警告,因为git将会腐蚀你的二进制文件。
如果 core.autocrlf被设置为false,没有进行过行尾结束的转换,那么文本文件就会按照原样存储。只要所有的开发人员都只是在linux或者windows上进行开发就会一切正常。但是以我的经验还是倾向于这种结尾的混合最终会导致问题。
我个人的偏好如果是windows开发人员还是设置为false.

/************* 翻译end *****************/
/******隔断********/

初次翻译,文字生硬,请见谅。

--追加于2014年1月3号

新的一年,我也改变改变,去年一直在学习node.js,这一不小心就学了两年了,今天想着要把一个node项目提交到csdn的code上面,谁知就遇到了这篇文章之前提到的字符转换的问题,心想这:哈哈这个我刚写的文章里面有啊,BUT,没那么简单,我将autocrlf设置为false时,并没有起什么作用,然后我想难不成要改成true?那就改,我去,问题依旧,那就input?感觉有点扯啊,那也改了,当然结果肯定是错误依旧。

我郁闷了,而且为了这个问题我也是困扰了整整两年的时间,因为之前在刚开始学习node的时候就想把这个项目托管,就是因为这个问题一直搁置,今天我是无论如何也要提交上去,我就不信了,SO~,我各种查资料,终于被我找到这样一个文章http://www.cnblogs.com/flying_bat/archive/2013/09/16/3324769.html

里面介绍到可以忽略混合字符,我爽快试了一下,见效很快啊,已经成功提交了。

最近在使用GitHub,发现不时没有修改过的文件要提交,对比发现文件全部修改,但找不到不一样的地方。
想可能是换行符的问题,因为Windows和Linux的换行符不一样,而Git默认应该是Linux的,今天Bing了下,果然是这个问题。


CR回车 LF换行Windows/Dos CRLF \r\n
Linux/Unix LF \n
MacOS CR \r
解决方法是:打开命令行,进行设置,如果你是在Windows下开发,建议设置autocrlf为true。


一、AutoCRLF
#提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true   


#提交时转换为LF,检出时不转换
git config --global core.autocrlf input   


#提交检出均不转换
git config --global core.autocrlf false
二、SafeCRLF


#拒绝提交包含混合换行符的文件
git config --global core.safecrlf true   


#允许提交包含混合换行符的文件
git config --global core.safecrlf false   


#提交包含混合换行符的文件时给出警告
git config --global core.safecrlf warn

-------------------------------

我个人认为可能是两种结尾字符都存在导致了不管是false还是true都会报错,所以干脆忽略,当然这个是我个人的项目,无所谓。

还有就是当遇到这类的字符转换的问题时候,最佳的解决方案是找到那个文件,将他的结尾字符手动改成你需要的,不过我觉得文件太多,而且要一个个的去检查,应该还没做完呢,就已经累死了吧。

不知道大家谁有更好的解决办法欢迎分享。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值