真的有必要拥有这个文件吗?
是的,对于与 Git 相关的任何设置(eol、diff、合并过滤器、内容过滤器……),您希望存储库的任何协作者都遵循。
这不同于git config
出于安全原因,它仍然是本地的(因为它可能包含敏感信息或危险指令)
A .gitattributes
是版本化源代码的一部分,有助于建立通用的 Git 标准。
例如,我总是把(如VonC/gitcred/.gitattributes https://github.com/VonC/gitcred/blob/main/.gitattributes):
*.bat text eol=crlf
*.go text eol=lf
因为无论你的 IDE/编辑器如何配置,我needCRLF 使我的 Windows bat 脚本能够正确运行,我更喜欢LF https://www.programming-books.io/essential/go/normalize-newlines-1d3abcf6f17c4186bb9617fa14074e48用于我在 Windows 或 Linux 上编辑的 Go 文件。我总是考虑本地设置,例如core.autocrlf
反模式,最好留给false https://stackoverflow.com/a/2354278/6309.
But a .gitattributes
可以声明许多其他 Git 元素:
-
, 用于翻译文件 https://github.com/BGforgeNet/bg2-tweaks-and-tricks/blob/e9ff1b7e618d0d909bd52c53b31258feef131ad9/.gitattributes
-
to 嵌入文件 SHA1 https://stackoverflow.com/a/1796675/6309 as in here https://github.com/Airamek/runit/blob/9272d0d150d4d83e4d10c706ce1ded0c2afd637c/src/runsvchdir.c#L9-L12=
-
,最显着的是使用Git LFS https://git-lfs.github.com/, as in here https://github.com/Legorobotdude/Arcade-FPS/blob/f31c62c42cda5f10d0de419703963c8c471b5091/.gitattributes,我用过它many https://stackoverflow.com/a/44793610/6309 times https://stackoverflow.com/a/40317246/6309 before https://stackoverflow.com/a/67898483/6309.
-
,至少避免比较二进制文件,或定义外部差异驱动程序 https://stackoverflow.com/a/53149114/6309
-
(using xfunname例如 https://github.com/ElenaKlatt/ChatApplication/blob/3227aa8d4896885fefaac0b32558b92d1d432468/.metadata/.plugins/com.aptana.portablegit.win32/doc/git/html/gitattributes.txt#L459-L467=):我提到他们在这里 https://stackoverflow.com/a/28111535/6309.
-
像unityyamlmerge https://stackoverflow.com/a/64463933/6309
-
定义什么
diff
and apply
应该考虑空格错误
- ...
The .gitattributes
file 不是“强制”的,而是 Git 工具箱中的一个有用工具,可以在项目代码库中安全地共享。
你甚至可以在裸存储库 https://www.saintsjd.com/2011/01/what-is-a-bare-git-repository/:
在 Git 2.43(2023 年第 4 季度)中,属性子系统学会了遵守attr.tree
指定读取哪棵树的配置.gitattributes
文件来自.
See commit 9f9c40c https://github.com/git/git/commit/9f9c40cf34c29d4ad700d9869435d159056fa6fb, commit 2386535 https://github.com/git/git/commit/2386535511d1181afd4e892e2a866ffe5e1d7d21 (13 Oct 2023) by John Cai (john-cai) https://github.com/john-cai.
(Merged by Junio C Hamano -- gitster -- https://github.com/gitster in commit 26dd307 https://github.com/git/git/commit/26dd307cfaabe3d3f0b01aad64969e76a317811a, 30 Oct 2023)
attr https://github.com/git/git/commit/2386535511d1181afd4e892e2a866ffe5e1d7d21:裸仓库时从 HEAD 读取属性
Signed-off-by: John Cai
动机44451a2 https://github.com/git/git/commit/44451a2e5eec5360378be23e2cdbd9ecee49e14e (attr
: 教 , 2023-05-06, Git v2.41.0-rc1 --merge https://github.com/git/git/commit/67a3b2b39f638872531324e03217fa58f7b9ad1e)(attr:教“--attr-source =”全局选项“git”,2023-05-06),是为了使其可以使用gitattributes
与裸存储库。
为了更容易阅读gitattributes
然而,在裸存储库中,我们只需制作HEAD:.gitattributes
默认值。
这与邮件映射的工作方式一致,8c473ce https://github.com/git/git/commit/8c473cecfd8835c2bdf34b323e1b2de620099c04 ("mailmap
:裸存储库中的默认 mailmap.blob”,2012-12-13,Git v1.8.2-rc0 --merge https://github.com/git/git/commit/3a3100a889cab5d3a1a590258304dacd188a17f6).
并且,仍然使用 Git 2.43(2023 年第 4 季度):
See commit 9f9c40c https://github.com/git/git/commit/9f9c40cf34c29d4ad700d9869435d159056fa6fb, commit 2386535 https://github.com/git/git/commit/2386535511d1181afd4e892e2a866ffe5e1d7d21 (13 Oct 2023) by John Cai (john-cai) https://github.com/john-cai.
(Merged by Junio C Hamano -- gitster -- https://github.com/gitster in commit 26dd307 https://github.com/git/git/commit/26dd307cfaabe3d3f0b01aad64969e76a317811a, 30 Oct 2023)
attr https://github.com/git/git/commit/9f9c40cf34c29d4ad700d9869435d159056fa6fb: add attr.tree
用于设置要从中读取属性的树状结构
Signed-off-by: John Cai
44451a2 https://github.com/git/git/commit/44451a2e5eec5360378be23e2cdbd9ecee49e14e (attr
: 教 , 2023-05-06, Git v2.41.0-rc1 --merge https://github.com/git/git/commit/67a3b2b39f638872531324e03217fa58f7b9ad1e) (attr: 教导“--attr-source=”全局选项“git
”,2023-05-06)提供了传递树状结构作为 attr 源的能力。
然而,在像我们在 GitLab 那样将 Git 存储库作为裸存储库提供服务的情况下,更容易指出--attr-source
通过设置一次即可将其设置为所有命令的 HEAD。
添加新配置attr.tree
这允许这样做。
git config
现在包含在其man page https://github.com/git/git/blob/9f9c40cf34c29d4ad700d9869435d159056fa6fb/Documentation/config/attr.txt#L1-L7:
attr.tree
对存储库中的树的引用,从中读取属性,
而不是.gitattributes
工作树中的文件。
在一个光秃秃的
存储库,默认为HEAD:.gitattributes
.
如果该值是
无法解析为有效的树对象,而是使用空树。
当。。。的时候GIT_ATTR_SOURCE
环境变量或--attr-source
使用命令行选项时,此配置变量无效。