如何让Git将文件视为二进制文件?

Cha*_*all 93 git

遇到中型项目的问题,因为git将它们视为文本和合并,因此Visual Studio项目文件会出现问题.我想将文件设置为二进制文件,以便git不会自动合并这些文件.

有没有办法做到这一点?

Mic*_*ild 121

是的,使用属性.在你的.gitattributes文件中放置这样的东西(如果它不存在则创建它):

*.sln binary
*.suo binary
*.vcxproj binary
Run Code Online (Sandbox Code Playgroud)

binary实际上是一个预定义的宏,相当于-diff -merge -text.

如果你想仍然能够看到差异,你可以使用:

*.sln -merge -text
Run Code Online (Sandbox Code Playgroud)

这样,*.sln文件将不会被合并,也不会被eol规范化,但同时也是差异化的.

  • @neves:您必须在仓库中本地创建,也可以在全局范围内创建一个,例如`〜/ .gitattributes`,然后运行`git config --global core.attributesfile〜/ .gitattributes`参见http://stackoverflow.com/问题/ 28026767 /我应该在哪里放置我的全局gitattributes文件 (3认同)
  • 这个文件在哪里? (2认同)

Oma*_*med 5

您应该在.gitattributes文件中定义二进制文件属性(如果它不存在则创建它),方法是将这些行放入其中,以防止它将其作为文本diff文件处理:

# Define binary file attributes.
# - Do not treat them as text.
# - Include binary diff in patches instead of "binary files differ."
*.sln     -text diff
*.suo     -text diff
*.vcxproj -text diff
*.gif     -text diff
*.gz      -text diff
*.ico     -text diff
*.jpeg    -text diff   
Run Code Online (Sandbox Code Playgroud)