如何让 Git 将文件视为二进制文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11162267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make Git treat a file as binary?
提问by Charles Randall
Having a problem with a medium sized project where visual studio project files keep having issues due to git treating them as text and merging. I'd like to just set the file as binary so that git won't auto merge these files ever.
在中型项目中遇到问题,由于 git 将它们视为文本并合并,因此 Visual Studio 项目文件一直存在问题。我只想将文件设置为二进制文件,以便 git 永远不会自动合并这些文件。
Is there a way to do this?
有没有办法做到这一点?
回答by Michael Wild
Yes, using attributes. Put something like this in your .gitattributes
file (create it if it doesn't exist):
是的,使用属性。将这样的内容放入您的.gitattributes
文件中(如果不存在则创建它):
*.sln binary
*.suo binary
*.vcxproj binary
Here binary
is actually a predefined macro, equivalent to -diff -merge -text
.
这里binary
实际上是一个预定义的宏,相当于-diff -merge -text
.
If you want to still be able to see the diff, you can use:
如果您仍然希望能够看到差异,可以使用:
*.sln -merge -text
This way, the *.sln
files won't be merged, not have eol normalized, but meanwhile diff-able.
这样,*.sln
文件不会被合并,不会被 eol 标准化,但同时是可区分的。
回答by Omar Alahmed
You should define binary file attributes in your .gitattributes
file (create it if it doesn't exist) by putting these lines in it, to prevent it to handle it as text diff file:
您应该在.gitattributes
文件中定义二进制文件属性(如果不存在则创建它),将这些行放入其中,以防止将其作为文本差异文件处理:
# 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