尽管 .gitattributes

问题描述

我已经看到了 gitattribute end of line setting,这是一个不同且非常奇怪的问题。我已经检查了我的 autocrlf 和 eol 配置,但它们没有设置

我使用的是 Mac。在我们的存储库中,我们有一个 .gitattributes 文件,如下所示:

*.sql text eol=lf
*.sh text eol=lf

这对 .sql 文件非常有效。但是,我们有一个名为 scripts/some-version.sql文件。它以 Unix lf 结尾签入。当我在 OS X 上查看它时,该文件立即转换为 dos crlf 结尾并标记为已更改。在 Windows 上检出相同的文件会给出 Unix lf 结尾。

如果我注释掉 .gitattributes 文件中的 .sql 行,我将停止将 .sql 文件检出为 crlf。是 .gitattributes 导致了这种行为。但是,Windows 用户会不小心再次开始检查 crlf 结尾。遗憾的是不能更改文件名。

我尝试向 .gitattributes 添加 .sql text eol=lf 行,但这没有帮助。

我们如何才能为我们的 Windows 用户提供一个 .gitattributes 而不会在 OS X 上弄乱这个文件的行尾?


每个请求。问题的演示以及 check-attr

macbookpro:postgres btilly$ git checkout scripts/
Updated 1 path from the index
macbookpro:postgres btilly$ git status scripts/3.20.08-UPDATE.sql 
On branch integration
Your branch is up to date with 'origin/integration'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   scripts/3.20.08-UPDATE.sql

no changes added to commit (use "git add" and/or "git commit -a")
macbookpro:postgres btilly$ git check-attr -a scripts/3.20.08-UPDATE.sql 
scripts/3.20.08-UPDATE.sql: text: set
scripts/3.20.08-UPDATE.sql: eol: lf
macbookpro:postgres btilly$ file scripts/3.20.08-UPDATE.sql 
scripts/3.20.08-UPDATE.sql: UTF-8 Unicode text,with very long lines,with CRLF line terminators

解决方法

您的文件很可能已经以 CRLF 行结尾签入,至少在某些地方是这样。出乎人们意料的是,编辑 .gitattributes 不会影响任何已签入存储库的文件,也不会影响工作树中的干净文件。

基本上,每当您对 .gitattributes 进行更改时,您都需要运行 git add --renormalize . 并提交这些更改。否则,您最终可能会处于不一致的状态。

另请注意,您的 .gitattributes 模式不适用于区分大小写的系统,因为您的模式使用小写 *.sql,而您的文件使用大写 .SQL。因此,您可能还想添加以下内容:

*.SQL text eol=lf

当然,您需要在运行 git add --renormalize . 之前执行此操作。