git commit 得到致命错误“致命:CRLF 将被 LF 替换”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20168639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:21:26  来源:igfitidea点击:

git commit get fatal error "fatal: CRLF would be replaced by LF in"

giteol

提问by aserww106

I'm using Ubuntu 13.10 x64, and I am working on a project that some developers are using Windows , I recently changed the git config core.eolto "lf" and core.autocrlfto "input" and core.safecrlfto "true". Since then, when I try to commit file into my local repository, I get this error:
fatal: CRLF would be replaced by LF in ......
From what I understand, if I set core.eolto "lf" and core.autocrlfto "input", git will automatically convert CRLF to LF, but why this error come out? How can I fix this problem?

我正在使用 Ubuntu 13.10 x64,并且我正在处理一些开发人员正在使用 Windows 的项目,我最近将 git config 更改core.eol为“lf”、core.autocrlf“input”和core.safecrlf“true”。从那时起,当我尝试将文件提交到本地存储库时,出现此错误:据
fatal: CRLF would be replaced by LF in ......
我所知,如果我设置core.eol为“lf”和core.autocrlf“input”,git 会自动将 CRLF 转换为 LF,但是为什么会出现此错误出去?我该如何解决这个问题?

Thank you.

谢谢你。

回答by VonC

This is a classic issue:

这是一个经典问题:

http://toub.es/sites/toub.es/files/styles/standard_article/public/field/image/firstcommit.png
(picture from Luis Tubes's blog post)

http://toub.es/sites/toub.es/files/styles/standard_article/public/field/image/firstcommit.png
(图片来自Luis Tubes博文

The usual fix is to convert those files yourself, with dos2unixor Swiss File Knife.

通常的解决方法是使用dos2unixSwiss File Knife自己转换这些文件。

I have always preferred to keep core.autocrlfto false, which means:

我一直喜欢以保持core.autocrlffalse,其意为:

git config --global core.autocrlf false

回答by almo

I had the same problem and tried the suggested solution with no success.

我遇到了同样的问题并尝试了建议的解决方案但没有成功。

I had to execute a second command to make it work:

我必须执行第二个命令才能使其工作:

$ git config --global core.autocrlf false
$ git config --global core.safecrlf false

回答by Arun

$ git config core.autocrlf false

回答by Yola

One may just try dos2unix:

可以试试 dos2unix:

dos2unix [filename]

回答by GreenRaccoon23

This happened to me on thousands of files. So I wrote a quick bash script to make dos2unixfix it for me. Someone else on Linux or Mac might find it useful.

这发生在我的数千个文件上。所以我写了一个快速的 bash 脚本来dos2unix为我修复它。Linux 或 Mac 上的其他人可能会发现它很有用。

#!/usr/bin/env bash

unwindows() {

  local errmsg
  local fpath

  # base case
  errmsg="$(git add . 2>&1)"
  if [[ $? -eq 0 ]]; then
    echo 'Successfully converted CRLF to LF in all files.'
    echo 'Successfully ran "git add .".'
    echo 'Done.'
    return 0
  fi

  fpath="${errmsg#*fatal: CRLF would be replaced by LF in }"
  fpath="${fpath%.*}"

  if [[ "${fpath}" == "${errmsg}" ]]; then
    err 'Regex failed. Could not auto-generate filename from stderr.'
    return 1
  fi

  if [[ ! -e "${fpath}" ]]; then
    err "Regex failed. '${fpath}' does not exist."
    return 1
  fi

  if ! dos2unix "${fpath}"; then
    err "Failed to run \"dos2unix '${fpath}'\"."
    return 1
  fi

  # recursive case
  unwindows
}

err() {
  local -r msg=""
  echo "${msg}" >&2
}

unwindows

Basically, it tries to do git add .. If the command fails, it grabs the name of the incompatible file from the error output. Then it runs dos2unixon that file. It keeps repeating this process until git add .works.

基本上,它试图做git add .. 如果命令失败,它会从错误输出中获取不兼容文件的名称。然后它dos2unix在该文件上运行。它不断重复这个过程直到git add .工作。

If you run this, you should see dos2unix: converting file xxx to Unix format...repeatedly. If you don't, it's not working, so just press ctrl+cor command+cto stop it.

如果你运行这个,你应该会dos2unix: converting file xxx to Unix format...反复看到。如果你不这样做,它就不起作用,所以只需按ctrl+ccommand+c来停止它。

回答by Karl.S

You need to add all files that git statusdisplays as modified:

您需要添加git status显示为已修改的所有文件:

git add file1
git add file2

And then commit your changes :

然后提交您的更改:

git commit

This will keep your local files as is, but will autocrlfthem on the remote repository.

这将使您的本地文件保持原样,但autocrlf它们将保留在远程存储库中。

回答by Kazma Arakaki

I faced same trouble and fixed with editing .gitattributesas below.

我遇到了同样的问题并通过.gitattributes如下编辑修复。

$ vim .gitattributes

comment out 2 lines in .gitattributes

注释掉 .gitattributes 中的 2 行

-* text=auto
-* text eol=lf
+# * text=auto
+# * text eol=lf

回答by Nickofthyme

FYI not sure if this applies to you but I was getting this error when accidentally trying to add all node_modulesto the staged changes. So actually .gitignoringthe node_modulessolved my problem.

仅供参考,我不确定这是否适用于您,但是当我不小心尝试将所有内容添加node_modules到分阶段更改时出现此错误。因此,实际上.gitignoringnode_modules解决我的问题。

回答by thetwopct

I'm on a Mac using Terminal and had this problem with an .htaccess file I was trying to commit, getting the fatal error:

我在使用终端的 Mac 上遇到了这个问题,我试图提交的 .htaccess 文件出现了致命错误:

fatal: CRLF would be replaced by LF in .htaccess

I wanted to fix the problem, like the OP requests, not just turn off a git flag, so I found this articlethat gives a perl command to fix the problem on a per file basis.

我想解决这个问题,就像 OP 请求一样,不仅仅是关闭一个 git 标志,所以我发现这篇文章给出了一个 perl 命令来在每个文件的基础上修复问题。

perl -pi -e 's/\r\n/\n/g' input.file

So for my .htaccess error above, I ran the following:

因此,对于上面的 .htaccess 错误,我运行了以下命令:

perl -pi -e 's/\r\n/\n/g' .htaccess 

The flags -p, -i and -e (pie) can be combined to allow you to edit files using Perl from the command line. In this case replacing all \r\n found with \n.

标志 -p、-i 和 -e(饼图)可以组合使用,以允许您从命令行使用 Perl 编辑文件。在这种情况下,将找到的所有 \r\n 替换为 \n。