gittextbinary

Override git's choice of binary file to text


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.


Solution

  • 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.