Skip to content

Instantly share code, notes, and snippets.

@nachocab
Last active February 13, 2022 15:45
Show Gist options
  • Save nachocab/6429893 to your computer and use it in GitHub Desktop.
Save nachocab/6429893 to your computer and use it in GitHub Desktop.
How to make git diff work perfectly with .docx files in Mac OS X (it even colors by word)
# download docx2txt by Sandeep Kumar
wget -O docx2txt.pl http://www.cs.indiana.edu/~kinzler/home/binp/docx2txt
# make a wrapper
echo '#!/bin/bash
docx2txt.pl $1 -' > docx2txt
chmod +x docx2txt
# make sure docx2txt.pl and docx2txt are your current PATH. Here's a guide
http://shapeshed.com/using_custom_shell_scripts_on_osx_or_linux/
mv docx2txt docx2txt.pl ~/bin/
# set .gitattributes (unfortunately I don't this can't be set by default, you have to create it for every project)
echo "*.docx diff=word" > .git/info/attributes
# add the following to ~/.gitconfig
[diff "word"]
binary = true
textconv = docx2txt
# add a new alias
[alias]
wdiff = diff --color-words
# try it
git init
# create my_file.docx, add some content
git add my_file.docx
git ci -m "Initial commit"
# change something in my_file.docx
git wdiff my_file.docx
# awesome!
@paulshapiro
Copy link

Pretty nice, thanks! Quick improvement to handle filenames with spaces -

the line

docx2txt.pl $1 -' > docx2txt

should say

docx2txt.pl "$1" -' > docx2txt

@jasmas
Copy link

jasmas commented Feb 10, 2014

Thanks! This is perfect for quickly viewing the differences between two docs and I find myself using it a lot.

I'm using git for repositories of docx files, so I took things a step further and wrote a git merge driver for the Mac which uses Word itself to resolve merge conflicts by opening the common ancestor and showing both local and remote differences using tracked changes. This way I can complete the merge by accepting or rejecting each change and making any other minor fixup edits.

Check it out: http://github.com/jasmas/wordMerge

@dannycanales
Copy link

One additional step I needed was chmod +x docx2txt.pl

@MichaelChirico
Copy link

git wdiff my_word.docx produced no output for me (no errors, nothing)

@molnarjoe
Copy link

I have Mac and Linux systems. And I got it to work on Linux Mint. without changing the instructions. Thanik you for sharing this.
I only have one question. The compare is showing me didn'tdidn't, the didn't is in red and the didn't is in green.
This is very confusing to look through hundreds of words with apostrophies. Can something be done about this?

@molnarjoe
Copy link

That's "didn't" vs "didn & apos ; t", with no spaces.
The forum software changed my text in the above question!!!
How do I get it to stop comparing & apos ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment