当 运行 on OS X 时,如何提交具有 LF 行结尾的文件,但在 CRLF 中保留某些文件类型?

How can I commit files with LF line endings, but keep certain file types in CRLF when running on OS X?

在我们的存储库中,行尾都是 LF。这很好。当检出到 Windows 时,core.autocrlf 通过将所有文件保存为本地 CRLF 使生活变得简单,但提交 LF 结尾。这个也不错

但是当我的本地副本在 OS X 上并且 Windows VM 正在处理一些文件(Visual Studio 用于网络文件)时,情况就不那么好了,因为 LF 把事情搞砸了。

所以我想要的是将存储库保留为 LF,并且本地只有 select 几种文件类型作为 CRLF。这可行吗?

  1. 确保回购已 core.autocrlf 设置:

    git config --global core.autocrlf true

  2. Create/modify 文件 $GIT_DIR/info/attributes 的行如下:

*.js      eol=crlf
*.css     eol=crlf
*.html    eol=crlf

我使用 Notepad++ 验证了这一点 - 本地文件有 CRLF,然后直接从 repo 主机下载的文件只显示 LF。

这是使用 .gitattributes 文件完成的,它允许您通过模式匹配文件名,并根据匹配分配预期的行为。

延伸阅读:

    # Declare files that will always have CRLF line endings on checkout.
    *.sln text eol=crlf