【问题标题】:Why does Git convert CRLF to LF on Linux?为什么 Git 在 Linux 上将 CRLF 转换为 LF?
【发布时间】:2014-07-01 12:37:24
【问题描述】:

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

【问题讨论】:

标签: linux git line-endings


【解决方案1】:

来自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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    相关资源
    最近更新 更多