Override git's choice of binary file to text

GitTextBinary

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

Look at Git Attributes -- they may be able to help you by specifying that a certain file extension is to be treated as text.

Solution 3 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRonaldBView Question on Stackoverflow
Solution 1 - GitGreg HewgillView Answer on Stackoverflow
Solution 2 - GitMark Leighton FisherView Answer on Stackoverflow
Solution 3 - GitdeadlydogView Answer on Stackoverflow