Skip to content

Instantly share code, notes, and snippets.

@thbar
Last active November 3, 2023 06:01
Show Gist options
  • Save thbar/4943276 to your computer and use it in GitHub Desktop.
Save thbar/4943276 to your computer and use it in GitHub Desktop.
How to diff PDF files with Git

One can use MD5 or plain text diff to see differences in PDF files. If that's not enough, here's how to use diff-pdf which knows how to diff based on appearance or words:

  • brew install diff-pdf
  • edit your ~/.gitconfig to add this:
[difftool "diffpdf"]
  cmd = diff-pdf --view \"$LOCAL\" \"$REMOTE\"
  • then use with:
git difftool --tool=diffpdf your_pdf_file.pdf

I suspect there's maybe some way to force always using a specific difftool for files with a specific extension; in the mean time I'm just using a bash function for this.

Also useful in .bash_profile:

function git_diff_pdf {
  yes | git difftool --tool=diffpdf $1
}

then you can use it like:

git_diff_pdf your_pdf_file.pdf
@sdutoit
Copy link

sdutoit commented Jun 9, 2023

With a recent version of git (I'm using 2.40.1) you can do this pretty easily using diff in both the git config and .gitattributes (official docs):

In your project's .gitattributes:

*.pdf diff=diff-pdf

In your ~/.gitconfig or your project's .git/config:

[diff "diff-pdf"]
        command = f() { diff-pdf --view "$2 $1;" } ";" f

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