How to force Git to consider a file as binary

If you are using Git on Win­dows and fol­low my ad­vise on how to get past the prob­lem with the “sus­pi­cious patch lines”, you might run into prob­lems if you are using En­cap­su­lated Post­Script (.eps) files in your repos­i­tory.

Post­Script files are al­most plain-text files, and if you set core.​autocrlf and core.​safecrlf, they might cause prob­lems with the EPS bi­nary en­coded parts, as they might be de­tected as text-files and there­fore re­move any CRLF and re­place it with sin­gle LF, which can mess up the whole image.

To force Git to con­sider a file bi­nary which it would con­sider as text-file oth­er­wise, the eas­i­est way is to add a .gitattributes file to the di­rec­tory con­tain­ing the file or to any par­ent di­rec­tory. In my case, I nor­mally add a .gi­tat­trib­utes file in the root of the repos­i­tory, con­tain­ing

*.eps -text -diff
*.jpg -text -diff
*.png -text -diff

In the file you set at­trib­utes to a path (or a pat­tern), or unset them (with the minus sign).  The text at­tribute is the at­tribute which tells that end-of-line nor­mal­iza­tion should be ap­plied to a file. If you unset it, Git won’t mess with the line end­ings in the file and con­sider it bi­nary.

More de­tails can be found on the gi­tat­trib­utes man page.

One thought on “How to force Git to consider a file as binary”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.