将 git 选择的二进制文件覆盖为文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14990993/
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
Override git's choice of binary file to text
提问by RonaldB
I've seen several question asking how to make git treat a text file as binary, but haven't seen the opposite yet:
我见过几个问题,询问如何让 git 将文本文件视为二进制文件,但还没有看到相反的情况:
How do I change git's choice of treating a text file as binary? I have a text file where in some configuration strings an EOT and ETX is used to separate parts of the configuration parameters.
如何更改 git 将文本文件视为二进制文件的选择?我有一个文本文件,其中在某些配置字符串中使用 EOT 和 ETX 来分隔部分配置参数。
For example, the source code contains lines like this:
例如,源代码包含这样的行:
INPUT 'ScrollRemote[EOT]no[ETX]NumDown[EOT]0[ETX]CalcWidth[EOT]no[ETX]MaxWidth[EOT]80[ETX]FetchOnReposToEnd[EOT]yes[ETX].....'
回答by Greg Hewgill
The way files are actually stored inside the Git repository is not relevant to how they are treated when displayed. So the git diff
program, when asked to compare two files, first obtains both complete files from the repository and then runs a difference algorithm against them.
文件在 Git 存储库中实际存储的方式与它们在显示时的处理方式无关。因此git diff
,当要求程序比较两个文件时,程序首先从存储库中获取两个完整文件,然后对它们运行差异算法。
Normally, git diff
looks for non-printable characters in the files and if it lookslike the file is likely to be a binary file, it refuses to show the difference. The rationale for that is binary file diffs are not likely to be human readable and will probably mess up your terminal if displayed.
通常,git diff
在文件中查找不可打印的字符,如果文件看起来很可能是二进制文件,则拒绝显示差异。这样做的理由是二进制文件差异不太可能是人类可读的,如果显示,可能会弄乱您的终端。
However, you can instruct git diff
to always treat files as text using the --text
option. You can specify this for one diff
command:
但是,您可以git diff
使用该--text
选项指示始终将文件视为文本。您可以为一个diff
命令指定此项:
git diff --text HEAD HEAD^ file.txt
You can make Git always use this option by setting up a .gitattributes
file that contains:
您可以通过设置.gitattributes
包含以下内容的文件来使 Git 始终使用此选项:
file.txt diff
The diff
attribute here means:
diff
这里的属性是指:
A path to which the
diff
attribute is set is treated as text, even when they contain byte values that normally never appear in text files, such as NUL.
diff
设置属性的路径被视为文本,即使它们包含通常不会出现在文本文件中的字节值,例如 NUL。
回答by Mark Leighton Fisher
Look at Git Attributes-- they may be able to help you by specifying that a certain file extension is to be treated as text.
查看Git 属性——它们可能会通过指定将某个文件扩展名视为文本来帮助您。
回答by deadlydog
If you are trying to compare or merge text files and git is saying they are binary files, they may just have different encoding (e.g. UTF-8 and ANSI). See the answer I gave on this post.
如果您尝试比较或合并文本文件并且 git 说它们是二进制文件,则它们可能只是具有不同的编码(例如 UTF-8 和 ANSI)。请参阅我在这篇文章中给出的答案。