繁体   English   中英

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

[英]Why does Git convert CRLF to LF on Linux?

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

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

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

git config --global core.autocrlf input

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM