为什么Git在Linux上将CRLF转换为LF?

3

我有一些需要使用LF行结尾的shell脚本(我使用的是Ubuntu 14.04)。由于这是一个跨平台的开源项目,因此使用了.gitattributes文件来使行结尾自动工作。不幸的是,它们并没有起到作用。

$ cat .gitattributes
# automatically normalize line endings
* text=auto

我在文件系统中有以下文件:

$ file extract.sh 
extract.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators

如果我现在使用 dos2unix 处理它,它会正确显示为 LF 终止符。
$ file extract.sh 
extract.sh: Bourne-Again shell script, ASCII text executable

此外,git 显示其已被修改:
$ git status
[..]
Changes not staged for commit:
[..]

    modified:   extract.sh

现在我将它添加到索引中:
$ git add extract.sh 
warning: LF will be replaced by CRLF in extract.sh.
The file will have its original line endings in your working directory.

但是新的 git status 显示如下:
$ git status .
[..]

nothing to commit, working directory clean

并且file显示它没有改变:

$ file extract.sh 
extract.sh: Bourne-Again shell script, ASCII text executable

然而,如果我将其移除并再次检查:
$ rm extract.sh 
$ git checkout extract.sh
$ file extract.sh 
extract.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators
$ git status .
[..]

nothing to commit, working directory clean

即使从缓存中删除也无济于事:
$ git rm --cached extract.sh
$ git reset --hard
$ file extract.sh 
extract.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators

100美元的问题是:我如何让git给我一个LF结尾的文件?


另请参阅此链接 - Dominik Antal
1个回答

4

来自http://git-scm.com/book/en/Customizing-Git-Git-Configuration

如果您使用的是Linux或Mac系统,并且使用LF行尾,那么您不希望Git在检出文件时自动将其转换; 但是,如果意外引入了带有CRLF行尾符的文件,则可能希望Git进行修复。 您可以通过将core.autocrlf设置为input告诉Git在提交时将CRLF转换为LF,而不是反过来。

git config --global core.autocrlf input

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接