Git 二进制文件与 .gitattributes 中的 -diff

标签 git

在 .gitattributes 中有以下等价物:

*.chm binary

*.chm -diff

我试过 *.chm二进制文件,但恐怕它仍会尝试 merge 这些文件。是 -diff更适合我打算做什么?

此外,在提交给定类型的文件后,此设置是否适用?

谢谢

最佳答案

谢谢你的问题,你问:

I have tried *.chm binary but I am afraid it may still try to merge these files. Is -diff more appropriate for what I intend to do ?


内置的宏属性二进制等效于:
[attr]binary -diff -merge -text
(引用: Defining Macro Attributes - Gitattributes )
所以从你的问题害怕-diffbinary可能更合适:差别不大。两者通常就足够了,gitattributes 文档名称 -diff作为将文件标记为二进制文件的最简单方法之一。那么为什么不去使用它 .chm文件?
请参阅以下示例,其中显示了问题中某些属性的效果:
.gitattributes file
[1]                     | [2]                   | [3]
------------------------|-----------------------|------------------------
*.chm binary            | *.chm -diff           | *.chm binary
*.chm -diff             |                       |

$ git check-attr -a help.chm
------------------------|-----------------------|------------------------
help.chm: binary: set   | help.chm: diff: unset | help.chm: binary: set
help.chm: diff: unset   |                       | help.chm: diff: unset
help.chm: merge: unset  |                       | help.chm: merge: unset
help.chm: text: unset   |                       | help.chm: text: unset

REMARK: [1] above is identical in output to the single-line *.chm -diff binary or *.chm binary -diff. A strong hint that all these are logically equivalent to just *.chm binary and different to *.chm -diff.


对于问题是否-diff更适合您的意图,这取决于您的配置中的其他 gitattributes 以及您认为合适与否。由于您没有在问题中分享它们,我可以认为它们是未定义的答案(这里没有冒犯,属性可以设置、未设置或未定义)。但是,答案将是不完整的。因此,一切皆有可能,并且使用二进制 marco 属性可能会取消设置或定义先前已设置或未定义的 merge 和/或已在存储库配置中有效的文本属性。
因此 binary看起来比 -diff 更可取对我来说只是为了按下一个答案(不太可能好),因为您担心 merge 二进制文件,它是 binary取消设置 merge 的宏属性具有以下后果的属性:

Unset

Take the version from the current branch as the tentative merge result, and declare that the merge has conflicts. This is suitable for binary files that do not have a well-defined merge semantics.


(引用: merge - Performing a three-way merge - Gitattributes )
这与使用二进制内置 merge 驱动程序相同。因此,就像二进制宏属性所建议的那样,您将 merge 明确标记为“二进制”。
-diff 不同- 这也隐式地将文件标记为二进制 - 二进制宏属性 更改 merge 属性并因此取消设置 diff 属性允许有一个不同于未设置 merge 属性的 merge 驱动程序在运行(这是现有答案正确的地方,使用 binary-diff 不同 - 它是不完整的列出宏思想的属性并错过了 merge 属性的未设置 - 它可能没有记录在使用思想的版本中,也许是 git 版本 < 1.7.12.4)。
正如您可能想象的那样,这些都是非常好的细节。总之-diff应该足够了,binary可能不会受到伤害,但会取消设置比通常需要的更多的属性,因此在 merge 时更加明确(没有提到 text 属性),但可能会产生不需要的副作用和/或使 future 的维护变得更加困难(这是非常理论上的,但是它可能会做更多让你害怕的事情)。
我个人的回答是,不要害怕,按照自己的喜好来-diff直到遇到实际问题。很可能你很久以前就已经解决了这个问题。

也感谢您提出的其他问题:

Also, will this setting be applicable after a file of the given type has already been committed ?


是的。
如有疑问,请尝试使用 $GIT_DIR/info/attributes (具有最高优先级)因为它是您的克隆本地的并且从不提交,然后使用 git check-attr 进行验证它如何影响已提交文件的属性。
Git 不会将每个提交的文件的属性存储在存储库中,仅存储在 .gitattributes 中。文件/s,如果它/他们是/已提交。

关于Git 二进制文件与 .gitattributes 中的 -diff,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58918555/

相关文章:

git - msysgit bash shell-如何解决 "cannot find command"

git - Mavericks 和 Xcode 5.0.1 之后使用 git mergetool 和 opendiff 时出错

git - 我将 SourceTree 与 Git 结合使用,但需要使用 SVN 存储库,我有哪些选项?

git - merge 没有通用历史记录但具有通用文件的git存储库

git - 递归忽略git中的文件夹

java - 使用功能分支进行 Gradle 依赖管理

bash - 如何使 bash 完成为 git 别名工作?

java - Android Studio 中的某些 Java 文件未显示在 GitHub Desktop 上

git "bad sha1 file"错误

RadStudio/Delphi 10 中的 Git 集成