将 git 选择的二进制文件覆盖为文本 [英] Override git's choice of binary file to text

查看:20
本文介绍了将 git 选择的二进制文件覆盖为文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过几个问题,询问如何让 git 将文本文件视为二进制文件,但还没有看到相反的情况:

I've seen several question asking how to make git treat a text file as binary, but haven't seen the opposite yet:

如何更改 git 将文本文件视为二进制文件的选择?我有一个文本文件,其中在某些配置字符串中使用 EOT 和 ETX 来分隔部分配置参数.

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.

例如,源代码包含这样的行:

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].....'

我希望将此文件视为文本,而不是二进制文件,以便我可以看到行更改的差异.

I would like this file to be treated as text, not a binary, so that I can see a diff of line changes.

推荐答案

文件在 Git 存储库中的实际存储方式与它们在显示时的处理方式无关.所以 git diff 程序,当被要求比较两个文件时,首先从存储库中获取两个完整的文件,然后对它们运行差异算法.

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 diff 会在文件中查找不可打印的字符,如果 看起来 该文件很可能是二进制文件,则它拒绝显示不同之处.这样做的理由是二进制文件差异不太可能是人类可读的,如果显示,可能会弄乱您的终端.

Normally, git diff looks for non-printable characters in the files and if it looks like 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.

但是,您可以使用 --text 选项指示 git diff 始终将文件视为文本.您可以为一个 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 HEAD HEAD^ file.txt

您可以通过设置 .gitattributes 使 Git 始终使用此选项 文件包含:

You can make Git always use this option by setting up a .gitattributes file that contains:

file.txt diff

这里的 diff 属性意味着:

The diff attribute here means:

设置了 diff 属性的路径被视为文本,即使它们包含通常不会出现在文本文件中的字节值,例如 NUL.

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.

这篇关于将 git 选择的二进制文件覆盖为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆