返回资讯中心
外部精选
软件工程
#开发者实时流

Git 文本文件 CRLF 转换的陷阱

来源标题:Footguns in Git CRLF conversions of text files

Git 的 text=auto 属性在 Linux 和 macOS 上会将工作区中的 CRLF 自动转换为 LF,导致文件在下次 Git 操作时被改写。文章举例显示,file3 在索引中为 LF、工作区为 CRLF,触发 CRLF 将被替换为 LF 的警告;而 file1 和 file2 因在 .gitattributes 创建前就已加入索引,未发生自动转换。作者指出,被识别为二进制的 file4 同样被设置了 text 属性,虽符合逻辑但容易让人困惑。

Hacker News 最新AgoRapide2026/11/16 · 今天12 分钟阅读

以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。

warning: in the working copy of 'file3', CRLF will be replaced by LF the next time Git touches it i/lf w/lf attr/text=auto .gitattributes i/crlf w/crlf attr/text=auto file1 i/crlf w/crlf attr/text=auto file2 i/lf w/crlf attr/text=auto file3 i/-text w/-text attr/text=auto file4 这表明,如上一例所示,file3 被自动转换成了 LF 而不是 CRLF(该警告的解释见下一段)。file1 和 file2 没有被自动转换,因为它们是在 .gitattributes 创建之前就已被加入索引的。 主要区别在于,git 现在会显示每个文件上所设置的属性。在这个例子中,我们对所有文件使用了相同的 text 属性,但你也可以针对单个文件类型分别设置。 请注意,即使 file4 被识别为二进制文件,text 属性也照样被设置在了它上面。这合乎逻辑(该属性适用于所有文件),但乍一看可能有点让人困惑。 我们现在还会立刻收到警告:warning: in the working copy of 'file3', CRLF will be replaced by LF the next time Git touches it(假设这是在 Linux 或 macOS 系统上运行的)。这是因为 file3 不符合 text=auto 属性——在 Linux / macOS 上,该属性的含义是“索引中的 LF 应当对应工作区中的 LF”(在 Windows 上你不会收到这个警告,因为对该系统而言,索引中为 LF、工作区中为 CRLF 才是“正确”的状态)。 如上一例所述,这个警告可能会让人困惑。如何解决请见后文。 如果我们修改 file1(但不改动其行尾符)并再次添加它: printf "B\x0D\x0A" > file1 git add -A git ls-files --eol 那么我们仍会看到(如上一例所示): i/lf w/lf attr/text=auto .gitattributes i/crlf w/crlf attr/text=auto file1 i/crlf w/crlf attr/text=auto file2 i/lf w/crlf attr/text=auto file3 i/-text w/-text attr/text=auto file4 如上一例所示,此时提交该文件也没有用。一个文件最初被加入索引之后,git 并不会自行改变索引中的行尾符。 如果我们“手动”把该文件改成 LF,那么索引中也会随之改变: printf "C\x0A" > file1 git add -A git ls-files --eol 响应: i/lf w/lf attr/text=auto .gitattributes i/lf w/lf attr/text=auto file1 i/crlf w/crlf attr/text=auto file2 i/lf w/crlf attr/text=auto file3 i/-text w/-text attr/text=auto file4 git add --renormalize 推荐的方法(如前面第二个例子所示)是使用 git add --renormalize: git add --renormalize -A git ls-files --eol 响应: i/lf w/lf attr/text=auto .gitattributes i/lf w/lf attr/text=auto file1 i/lf w/crlf attr/text=auto file2 i/lf w/crlf attr/text=auto file3 i/-text w/-text attr/text=auto file4 可以看到 file2 现在被改成了 LF(但只在索引中,如前所述,git add --renormalize 不会改变工作区)。 为了一致起见,我们把 file1 在工作区中改回 CRLF: printf "D\x0D\x0A" > file1 git add -A git ls-files --eol 响应: i/lf w/lf attr/text=auto .gitattributes i/lf w/crlf attr/text=auto file1 i/lf w/crlf attr/text=auto file2 i/lf w/crlf attr/text=auto file3 i/-text w/-text attr/text=auto file4 file1 在索引中仍是 LF,这反映了 text=auto 属性。换句话说,当它从工作区被复制到索引时,被转换成了 LF。 file1、file2 和 file3 现在处于一种对 Linux 系统来说不自然的状态。它们会伴随相应的警告:in the working copy of 'fileX', CRLF will be replaced by LF the next time Git touches it。 如上一例所示,仅靠简单的 git checkout 无法解决这个问题。在那个例子中,我们先修改文件,然后执行 git checkout。这里我们选择先提交,再删除并检出文件: git commit -m "X" rm file* git checkout . git ls-files --eol 响应: i/lf w/lf attr/text=auto .gitattributes i/lf w/lf attr/text=auto file1 i/lf w/lf attr/text=auto file2 i/lf w/lf attr/text=auto file3 i/-text w/-text attr/text=auto file4 这可能有点反直觉。 另外请注意,你无法使用 git add --renormalize 来做到这一点,因为它从不改变工作区,只改变索引。 如果我们删除 .gitattributes 文件,那么已修改的文件会再次“原样”被加入索引: rm .gitattributes printf "E\x0D\x0A" > file1 git add -A git ls-files --eol 响应: i/crlf w/crlf attr/ file1 i/lf w/lf attr/ file2 i/lf w/lf attr/ file3 i/-text w/-text attr/ file4 file1 被“原样”加入了索引。还要注意,由于我们删除了 .gitattributes,这些属性现在也不存在了。 这一次没有什么需要重新规范化的,因为索引与工作区一致,也没有属性作出与此不同的规定。 场景:一个 Windows 下的 .txt 文本文件本应是 CRLF 而不是 LF,但修复却无法提交。

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →