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

时间:2014-07-01 12:37:25

标签: linux git line-endings

我有一些需要有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终止文件?

1 个答案:

答案 0 :(得分:4)

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

  

如果您使用的是使用LF行结尾的Linux或Mac系统,那么   不希望Git在您签出文件时自动转换它们;   但是,如果意外引入了CRLF结尾的文件,   然后你可能希望Git修复它。您可以告诉Git将CRLF转换为   通过将core.autocrlf设置为, LF提交但不是相反   输入:

git config --global core.autocrlf input