git - Windows git "warning: LF will be replaced by CRLF",警告尾部向后吗?

标签 git

环境:

  • window 7
  • 管理系统

当我 git commit 时,它说:

warning: LF will be replaced by CRLF. 

这个警示尾部是不是反了?
我在Windows下编辑文件,行尾是CRLF,如下图:
enter image description here
git 将其更改为 LF 以提交 repo 协议(protocol)。
所以我认为正确的警告是:

warning: CRLF will be replaced by LF. 

最佳答案

warning: LF will be replaced by CRLF.

根据您使用的编辑器,带有 LF 的文本文件不必使用 CRLF 保存:最近的编辑器可以保留 eol 样式。但是那个 git 配置设置坚持要改变那些......

只需确保(如 I recommend here ):

git config --global core.autocrlf false

这样,您就可以避免任何自动转换,并且仍然可以通过 .gitattributes file 指定它们和 core.eol directives .


windows git "LF will be replaced by CRLF"

备注:the warning message has changed with Git 2.37 (Q3 2022)

Is this warning tail backward?

不:你在 Windows 上,git config help page确实提到

Use this setting if you want to have CRLF line endings in your working directory even though the repository does not have normalized line endings.

如“git replacing LF with CRLF”中所述,它应该只发生在 checkout 时(不提交),core.autocrlf=true

       repo
    /        \ 
crlf->lf    lf->crlf 
 /              \    

XiaoPeng 中所述的 answer ,该警告与:

warning: (If you check it out/or clone to another folder with your current core.autocrlf configuration,) LF will be replaced by CRLF
The file will have its original line endings in your (current) working directory.

git-for-windows/git issue 1242 中所述:

I still feel this message is confusing, the message could be extended to include a better explanation of the issue, for example: "LF will be replaced by CRLF in file.json after removing the file and checking it out again".

注意:Git 2.19(2018 年 9 月),当使用 core.autocrlf 时,伪造的“LF 将被 CRLF 取代”警告现在被抑制


作为quaylar没错comments ,如果提交时有转换,则仅转换为 LF

特定警告“LF 将被 CRLF 替换”来自 convert.c#check_safe_crlf() :

if (checksafe == SAFE_CRLF_WARN)
  warning("LF will be replaced by CRLF in %s.
           The file will have its original line endings 
           in your working directory.", path);
else /* i.e. SAFE_CRLF_FAIL */
  die("LF would be replaced by CRLF in %s", path);

它由convert.c#crlf_to_git()调用, 本身由 convert.c#convert_to_git() 调用, 本身由 convert.c#renormalize_buffer() 调用.

最后一个 renormalize_buffer() 只被 merge-recursive.c#blob_unchanged() 调用.

所以我怀疑只有当所述提交是 merge 过程的一部分时,这种转换才会发生在 git commit 上。


注意:对于 Git 2.17(2018 年第 2 季度),代码清理增加了一些解释。

参见 commit 8462ff4 (2018 年 1 月 13 日)作者:Torsten Bögershausen (tboegi) .
(由 Junio C Hamano -- gitster -- merge 于 commit 9bc89b1 ,2018 年 2 月 13 日)

convert_to_git(): safe_crlf/checksafe becomes int conv_flags

When calling convert_to_git(), the checksafe parameter defined what should happen if the EOL conversion (CRLF --> LF --> CRLF) does not roundtrip cleanly.
In addition, it also defined if line endings should be renormalized (CRLF --> LF) or kept as they are.

checksafe was an safe_crlf enum with these values:

SAFE_CRLF_FALSE:       do nothing in case of EOL roundtrip errors
SAFE_CRLF_FAIL:        die in case of EOL roundtrip errors
SAFE_CRLF_WARN:        print a warning in case of EOL roundtrip errors
SAFE_CRLF_RENORMALIZE: change CRLF to LF
SAFE_CRLF_KEEP_CRLF:   keep all line endings as they are

请注意 8462ff4 中引入的回归("convert_to_git(): safe_crlf/checksafe 变为 int conv_flags", 2018-01-13, Git 2.17.0) 回到 Git 2.17 循环导致 autocrlf 重写以生成警告信息 尽管设置了 safecrlf=false

参见 commit 6cb0912 (2018 年 6 月 4 日)作者:Anthony Sottile (asottile) .
(由 Junio C Hamano -- gitster -- merge 于 commit 8063ff9 ,2018 年 6 月 28 日)

关于git - Windows git "warning: LF will be replaced by CRLF",警告尾部向后吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17628305/

相关文章:

Git 重新标记理智和疯狂的建议 - 第 2 部分

git - 为什么 'git ls-files' 没有在 Tab 完成时显示?

linux - 无法在nodejs中执行git命令

git - 如何镜像和同步 GitHub 和 SourceForge 存储库?

Git:揭示潜在的 merge/ rebase 冲突

git - 如何在jenkins shell中重复添加git远程存储库名称?

git-grep 不使用多线程

.net - 如何用 libgit2sharp 处理 'git add *'?

windows - 在 Windows 中跨本地文件系统的 GIT 克隆 repo

git - 显示未在 git 中暂存提交的更改