Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Jan 19, 2014

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

the line

docx2txt.pl $1 -' > docx2txt

should say

docx2txt.pl "$1" -' > docx2txt
@jasmas

This comment has been minimized.

Copy link

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

@dan956

This comment has been minimized.

Copy link

commented Feb 4, 2015

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

@MichaelChirico

This comment has been minimized.

Copy link

commented Feb 11, 2016

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

@molnarjoe

This comment has been minimized.

Copy link

commented Aug 7, 2016

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

This comment has been minimized.

Copy link

commented Aug 8, 2016

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
You can’t perform that action at this time.