今天用Git bash遇到的问题,看了几个回答之后发现一个比较有价值的,给大家分享一下,其他很多的回答都有很或多或少存在一些弊端。
原回答地址在stackoverflow上,附上链接--http://stackoverflow.com/questions/1967370/git-replacing-lf-with-crlf
这里我把主要的东西提炼一下翻译成中文供大家参考。
首先问题出在不同操作系统所使用的换行符是不一样的,下面罗列一下三大主流操作系统的换行符:
- Uinx/Linux采用换行符LF表示下一行(LF:LineFeed,中文意思是换行);
- Dos和Windows采用回车+换行CRLF表示下一行(CRLF:CarriageReturn LineFeed,中文意思是回车换行);
- Mac OS采用回车CR表示下一行(CR:CarriageReturn,中文意思是回车)。
在Git中,可以通过以下命令来显示当前你的Git中采取哪种对待换行符的方式
此命令会有三个输出,“true”,“false”或者“input”
为true时,Git会将你add的所有文件视为文本文件,将结尾的CRLF转换为LF,而checkout时会再将文件的LF格式转为CRLF格式。
为false时,line endings不做任何改变,文本文件保持其原来的样子。
为input时,add时Git会把CRLF转换为LF,而check时仍旧为LF,所以Windows操作系统不建议设置此值。
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.
解决办法:
将core.autocrlf设为false即可解决这个问题,不过如果你和你的伙伴只工作于Windows平台或者Linux平台,那么没问题,不过如果是存在跨平台的现象的话,还是需要考虑一下。
但当 core autocrlf为true时,还有一个需要慎重的地方,当你上传一个二进制文件,Git可能会将二进制文件误以为是文本文件,从而也会修改你的二进制文件,从而产生隐患。
PS:
附上修改autocrlf的命令,以改为true为例:
- $ git config --global core.autocrlf true #true的位置放你想使autocrlf成为的结果,true,false或者input
$ git config --global core.autocrlf true #true的位置放你想使autocrlf成为的结果,true,false或者input