git - .gitattributes : override settings for a single file

标签 git gitattributes

我有一个 .txt 文件目录,我想将其视为二进制文件。这可以防止 git 更改这些文件的行结尾,以便校验和在所有系统上保持一致。

但是,我有一个 .txt 文件,我想将其保留为文本。如果我创建一个 .gitattributes 文件,例如:

*.txt binary
foo.txt text

foo.txt 仍被视为二进制文件,并且 git diff 无法正确显示更改。似乎 .gitattributes 中不支持否定,就像 .gitignore 中一样。还有其他方法可以实现此功能吗?

最佳答案

二进制是什么the gitattributes documentation指的是宏属性:

Setting the "binary" attribute also unsets the "text" and "diff" attributes as above.

所以binary字面意思是-text -diff。然后,您使用第二行覆盖 foo.txt-text 设置,但尚未覆盖 -diff 设置。将第二行更改为:

foo.txt text !diff

将恢复 diff 的默认未设置性,同时:

foo.txt text diff

将强制设置diff(两者可能都会产生差异,因为Git之前可能没有将foo.txt检测为二进制文件)。

(请注意,除了分段之外,没有直接的方法可以“撤消”宏属性。您可以定义自己的宏属性来撤消某些现有宏属性的每一部分,例如,定义一个表示 !text ! diff,然后它将删除binary。)

关于git - .gitattributes : override settings for a single file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71506502/

相关文章:

git - 致命 : Invalid ID Jenkins IIS

ios - 最佳实践 : how to add an external framework to xcode project which is under version control?

svn - 使用Git和SVN,以Git为中心服务器

git - 根据文件类型选择 git mergetool

git - 如何在 git 上使用 CRLF 提交文件?

git - 在提交之前准备一个 git commit 消息?

git - "cloned repository' 的当前事件分支是什么”

git - 使用 .gitattributes 使 git 存储带有 CRLF 行结尾的文本文件

git - .gitattributes 模式中的空格

git - 向 .gitattributes 添加评论/备注