Use Git for your PDF and Word .doc files!

Afik Cohen
Aug 5, 2014 · 2 min read

Sure, you’d love to do this, who wouldn’t? Word and PDF are awful file formats, but they’re still widespread and if you’re housing your important documents in them, and you’re like me, then you want them version controlled. Git can handle any binary file of course, but where’s the fun in seeing this?

$ git diff 
diff --git a/chapter1.doc b/chapter1.doc
index 88839c4..4afcb7c 100644
Binary files a/chapter1.doc and b/chapter1.doc differ

Enter .gitattributes.

Step by Step setup

Stick the following line in a .gitattributes file in your repo:

*.doc diff=word

I’ll let them explain:

This tells Git that any file that matches this pattern (.doc) should use the “word” filter when you try to view a diff that contains changes. What is the “word” filter? You have to set it up. Here you’ll configure Git to use the catdoc program, which was written specifically for extracting text from a binary MS Word documents … to convert Word documents into readable text files, which it will then diff properly:

$ git config diff.word.textconv textract

I deleted the stuff about catdoc because that project’s dead. Let’s use the lovely library I recently discovered instead.

$ pip install textract

For it to work with .doc files, we’ll need antiword installed too:

$ brew install antiword

Then let’s run a git diff:

Image for post
Image for post

Voila!


Originally published at on August 5, 2014.

aphex.cx

aphex.cx

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store