我们如何添加一个项目级别的配置 .gitattributes 文件来强制执行适用于任何克隆 repo 的行结尾?

How can we add a project level configuration .gitattributes file to enforce line endings which will be applicable to anyone cloning the repo?

我们在一个项目中工作,该项目的代码在 git 中并且使用 visual studio 开发。但我们经常遇到这样的问题,即由于行尾自动更改而导致整个文件发生更改。我们确实考虑过在每台开发人员机器上的 git 中配置行尾设置的选项,但我们希望将某些东西更紧密地集成到我们正在使用的 repo 中。 我能够在 git 文档 (https://git-scm.com/docs/gitattributes) 中找到解决方案,但现在我卡住了,因为我无法将此文件提交到存储库,因为 .git 文件夹在存储库之外. 我试图为此文件执行 "git add",但那也不起作用。

有什么建议吗?

您可以将 .gitattributes 文件放在项目工作树的根目录下。 .git文件夹的位置与它无关。

将 .gitattributes 放在项目根目录中 text eol=lf 对我有用

我确认:

  • 您可以在根文件夹中添加并提交 .gitattributes 个文件
  • 您不能通过符号链接(符号链接)引用存储在存储库外部的同一文件。

Git 2.32(2021 年第 2 季度)对于最后一点非常明确:

commit 8ff06de, commit bb6832d, commit 801ed01, commit 1cb12f3, commit a1ca398 (03 May 2021), and commit 43a2220, commit 0282f67, commit 9e1947c, commit 963d02a (01 May 2021) by Jeff King (peff)
(由 Junio C Hamano -- gitster -- in commit 416449e 合并,2021 年 5 月 11 日)

docs: document symlink restrictions for dot-files

Suggested-by: Philip Oakley
Signed-off-by: Jeff King

We stopped allowing symlinks for .gitmodules files in 10ecfa7 (verify_path: disallow symlinks in .gitmodules, 2018-05-04, Git v2.18.0-rc0 -- merge listed in batch #7) (verify_path: disallow symlinks in .gitmodules, 2018-05-04), and we stopped following symlinks for .gitattributes, .gitignore, and .mailmap in the commits from 204333b ("Merge branch 'jk/open-dotgitx-with-nofollow'", 2021-03-22, Git v2.32.0 -- merge).
The reasons are discussed in detail there, but we never adjusted the documentation to let users know.

This hasn't been a big deal since the point is that such setups were mildly broken and thought to be unusual anyway.
But it certainly doesn't hurt to be clear and explicit about it.

gitattributes 现在包含在其 man page 中:

NOTES

Git does not follow symbolic links when accessing a .gitattributes file in the working tree.

This keeps behavior consistent when the file is accessed from the index or a tree versus from the filesystem.

.gitignore, .mailmap and .gitmodules 相同。