git提示“warning: LF will be replaced by CRLF”

windows平台下使用git add,git deploy 文件时经常出现“warning: LF will be replaced by CRLF” 的提示

Git 可以在你提交时自动地把回车(CR)和换行(LF)转换成换行(LF),而在检出代码时把换行(LF)转换成回车(CR)和换行(LF)。 你可以用 git config --global core.autocrlf true 来打开此项功能。 如果是在 Windows 系统上,把它设置成 true,这样在检出代码时,换行会被转换成回车和换行:

#提交时转换为LF,检出时转换为CRLF
$ git config --global core.autocrlf true

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这篇博客: 【Cygwin64 Terminal】git 拉的代码提示:warning: LF will be replaced by CRLF中的 问题如下 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:
    • 一台电脑上,使用【Cygwin64】正常的 make menuconfig
    • 另一台电脑,git pull 代码后,发现各种错误,开始以为是Cygwin64没有安装全工具包,后面发现原来就是回车换行引起sh 文件无法正确运行引起的
    $ git diff
    warning: LF will be replaced by CRLF in .config.
    The file will have its original line endings in your working directory
    warning: LF will be replaced by CRLF in .config.old.
    The file will have its original line endings in your working directory
    warning: LF will be replaced by CRLF in .kconfig.d.
    The file will have its original line endings in your working directory
    warning: LF will be replaced by CRLF in include/autoconf.h.
    The file will have its original line endings in your working directory
    warning: LF will be replaced by CRLF in scripts/Kbuild.
    The file will have its original line endings in your working directory
    warning: LF will be replaced by CRLF in scripts/basic/.docproc.cmd.
    The file will have its original line endings in your working directory
    warning: LF will be replaced by CRLF in scripts/basic/.fixdep.cmd.
    The file will have its original line endings in your working directory
    warning: LF will be replaced by CRLF in scripts/basic/.split-include.cmd.
    The file will have its original line endings in your working directory
    warning: LF will be replaced by CRLF in scripts/gen_build_files.sh.
    The file will have its original line endings in your working directory
    
    • 不要让 git 改变回车换行,也就是Linux 本身的回车换行,拉下来时 windows git 可以增加\r,但是提交时,不需要增加\r。
    • 以下大的错误,看似很多,就是 sh文件,被增加\r引起的。
    $ make menuconfig
      HOSTCC  scripts/kconfig/conf.o
      HOSTCC  scripts/kconfig/kxgettext.o
      SHIPPED scripts/kconfig/zconf.hash.c
      HOSTCC  scripts/kconfig/zconf.tab.o
      HOSTLD  scripts/kconfig/mconf
    /cygdrive/d/github/pluto-os_hello/scripts/kconfig/lxdialog/check-lxdialog.sh: line 3: $'\r': command not found
    /cygdrive/d/github/pluto-os_hello/scripts/kconfig/lxdialog/check-lxdialog.sh: line 5: syntax error near unexpected token `$'\r''
    'cygdrive/d/github/pluto-os_hello/scripts/kconfig/lxdialog/check-lxdialog.sh: line 5: `ldflags()
    /cygdrive/d/github/pluto-os_hello/scripts/kconfig/lxdialog/check-lxdialog.sh: line 3: $'\r': command not found
    /cygdrive/d/github/pluto-os_hello/scripts/kconfig/lxdialog/check-lxdialog.sh: line 5: syntax error near unexpected token `$'\r''
    'cygdrive/d/github/pluto-os_hello/scripts/kconfig/lxdialog/check-lxdialog.sh: line 5: `ldflags()
    /cygdrive/d/github/pluto-os_hello/scripts/kconfig/lxdialog/check-lxdialog.sh: line 3: $'\r': command not found
    /cygdrive/d/github/pluto-os_hello/scripts/kconfig/lxdialog/check-lxdialog.sh: line 5: syntax error near unexpected token `$'\r''
    'cygdrive/d/github/pluto-os_hello/scripts/kconfig/lxdialog/check-lxdialog.sh: line 5: `ldflags()
      HOSTCC  scripts/kconfig/lxdialog/checklist.o
    In file included from scripts/kconfig/lxdialog/checklist.c:24:
    scripts/kconfig/lxdialog/dialog.h:31:10: error: #include expects "FILENAME" or <FILENAME>
       31 | #include CURSES_LOC
    
    
  • 以下回答来自chatgpt:

    针对该问题,有以下几种解决方案:

    1. 直接忽略: 根据参考资料中的段落2,遇到这种问题可以直接忽略,不会对整体工作造成影响。如果只是一个警告,可以选择忽略。无需进行额外操作。

    2. 关闭自动换行符转换: 可以使用以下指令关闭自动换行符转换:

    git config --global core.autocrlf false

    这种方法已经在参考资料段落3中给出。

    1. 使用一个统一的换行符: 在项目中规定使用一个统一的换行符,这样就不会出现换行符错乱的问题。建议使用Unix的LF换行符。

    2. 使用Git LFS: Git Large File Storage (LFS)是一个Git扩展,允许您将大型二进制文件存储在单独的存储库中。Git LFS允许您将二进制文件存储在单独的 Git LFS 存储库中,而不是与代码在同一个存储库中。这样可以避免代码库过大,同时也方便管理。需要注意的是,LFS是面对企业级的收费项目,如果是个人使用可以选择一些免费的Git仓库。

    以上是针对该问题的几种解决方案,可以根据具体情况进行选择。其中第二种方法是针对普遍情况的最优解决方案。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^