Inspired by:
Although we can use git to track changes in almost any file, including binary files like .docx files, it is difficult to visualize the changes made to these files.
I’ve put together simple instructions that you can use to visualize changes using git diffs.
- Add pre-commit and post-commit hooks to keep a Markdown (.md) copy of all
.docxfiles inside this repository. This allows you to usegit diffsto show the changes in the document, which can be viewed in the terminal, on the repository's commit page, in emails, or in other places that supportgit diffs.
- You must install Pandoc (https://pandoc.org/) on your machine. Run
pandoc -vin your terminal to check if it is available.
-
Clone this repository:
It is recommended to include
--depth=1so your new repository will have a fresh history.Replace
FOLDER_NAMEwith your directory name:git clone --depth=1 https://github.com/farhan-syah/git-docx.git FOLDER_NAME cd FOLDER_NAME -
Copy the hooks into the
.git/hooksdirectory:cp -R hooks/* .git/hooks/ -
Update the local Git configuration to enable Pandoc-based diffs for
.docxfiles and add a useful alias for word-diff:Run the following commands inside the repository to update the local Git configuration:
git config diff.pandoc.textconv "pandoc --to=markdown" git config diff.pandoc.prompt false git config alias.wdiff "diff --word-diff=color --unified=1"
These settings will:
- Convert
.docxfiles to Markdown when generating diffs. - Disable the prompt when using Pandoc for diffs.
- Create a
wdiffalias for word-diff mode with colored output.
- Convert
-
Create or modify the
.docxfiles using proper.docxsoftware, like Microsoft Word or LibreOffice.WARNING: Do not create or modify the
.docxfiles using terminal or common text editors that have no.docxsupport, as it will break the file andpandocwill fail to read it. Remember,.docxis a binary file and not a normal text file.For UNSTAGED changes, you can view the difference directly by using
git wdiff. Please note that you'll see empty file when running this command on staged or commited files.:git wdiff FILENAME.docx
or you can simpy use
git gui blamefor both STAGED or UNSTAGED:git gui blame FILENAME.docx
-
Stage and commit the files:
git add FILENAME.docx git commit -m "Amend FILENAME.docx"When you stage the documents and create a new commit, a copy of the files in
.mdwill be created/updated, and you can utilize git tools to view the changes.
To view the history of changes made to the .md files (which are the Markdown copies of your .docx files), you can use a few different Git commands. Here’s how:
-
Using
git logto View the HistoryThe
git logcommand is the most straightforward way to view the commit history of a file:git log -- FILENAME.md
Replace
FILENAME.mdwith the name of the Markdown file you want to inspect. This command will show the commit history for that specific file. -
Viewing Differences with
git log -pIf you want to see the actual changes made to the file in each commit, you can use:
git log -p -- FILENAME.md
This will show you the commit messages along with the differences introduced in each commit.
-
Using
git diffto Compare VersionsIf you know the specific commits or branches you want to compare, you can use
git diff:git diff COMMIT1 COMMIT2 -- FILENAME.md
Replace
COMMIT1andCOMMIT2with the commit hashes or branch names you want to compare. This will show you the changes between the two versions of the file. -
Using
git wdifffor Word-Level DiffsIf you set up the
wdiffalias (as previously discussed), you can use it to view word-level differences in the.docxfiles:git wdiff COMMIT1 COMMIT2 -- FILENAME.docx
This will give you a more granular view of the changes, highlighting individual word changes within the lines.
-
Viewing History in a GUI
If you prefer a graphical interface, you can use tools like
gitk,GitHub Desktop,VSCode Git Extensionsor any Git client that supports visual diffs. For example:gitk FILENAME.md
This command opens a graphical history viewer for the specific file, showing commits and diffs visually.
- View commit history:
git log -- FILENAME.md - View commit history with diffs:
git log -p -- FILENAME.md - Compare two versions:
git diff COMMIT1 COMMIT2 -- FILENAME.md - Word-level diffs:
git wdiff COMMIT1 COMMIT2 -- FILENAME.docx - GUI view (gitk):
gitk FILENAME.docxorgitk FILENAME.md - GUI view (git gui):
git gui blame FILENAME.docxorgit gui blame FILENAME.md
These commands will help you track and inspect the history of changes made to your Markdown files in the repository.