Ricardo Amores
Ricardo Amores

Reputation: 4687

Git: make text files as binary while still showing a text diff

I have some special files in my repo that although they are text files they are marked as binary in .gitattributes so they don't get automatically merged if we have a conflict: I need to choose between one version of the file or another. (*)

However it still would be useful to show the text diff of those files in order to make a decision or to see the changes between different commits. I can do that using an external diff tool, and github actually shows the text diff, but I wonder if I can configure git so its internal diff tool work that way.

(*) We are working with Unity3D and storing its internal data files as text (prefabs, scenes, etc) so they are compressed efficiently in the repo.

Upvotes: 4

Views: 292

Answers (2)

Alex Wolf
Alex Wolf

Reputation: 20148

You could define a special diff filter for these files.

To achieve this you would add the following configuration to your .gitconfig file: (The name binary-text is arbitrary)

[diff "binary-text"]
     textconv = cat

textconv defines the tool to convert the binary files to usual text, since your files are usual text files a simple cat should be sufficient. To enable diffing you have to specifiy the diff filter for the file types.

So lets say the files end on .foo.

Now add the following to your .gitattribute file:

*.foo diff=binary-text

Now diff should work as expected.

For more information on this you can read the Customizing Git - Git Attributes chapter from the Pro Git book.

Upvotes: 0

Jan Krüger
Jan Krüger

Reputation: 18530

binary is a macro that disables diffing, line ending normalization, and merging. You seem to really only need the latter. For that, try using -merge instead. If you want to disable line ending normalization, too, use -merge -text.

Upvotes: 3

Related Questions