Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override git's choice of binary file to text

Tags:

git

text

binary

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

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.

like image 517
RonaldB Avatar asked Feb 20 '13 22:02

RonaldB


People also ask

Can git diff binary files?

Diffing binary filesYou can do this by creating a . gitattributes file in the root of your repository. Once configured, git diff will first run the binary file through the configured converter script and diff the converter output.

When should text files be preferred over binary files?

When file does need to be read by people or need to be ported to a different type of system text files should be preferred over binary files.

Are binary files more efficient than text files?

One of the advantages of binary files is that they are more efficient. In terms of memory, storing values using numeric formats such as IEEE 754, rather than as text characters, tends to use less memory.


1 Answers

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.

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.

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 

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

file.txt diff 

The diff attribute here means:

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.

like image 178
Greg Hewgill Avatar answered Sep 20 '22 09:09

Greg Hewgill