Skip to content

Instantly share code, notes, and snippets.

@PrabhatKJena
Last active June 25, 2023 03:05
Show Gist options
  • Save PrabhatKJena/0884644ae01a49a9819aebd883e54003 to your computer and use it in GitHub Desktop.
Save PrabhatKJena/0884644ae01a49a9819aebd883e54003 to your computer and use it in GitHub Desktop.
How to git diff for MS-Excel files(xls/xlsx) on Mac-OS

How to git diff for MS-Excel files(xls/xlsx) on Mac-OS

Installation

Prerequisites

  • Requires Java 1.6 or higher.
  • Assumes Java is added to PATH (to check open a cmd and run java -version)
  • No other platform specific requirements
  • A shell script and a bat script are packaged

Just download the zip file.

Extract it anywhere (and optionally you add the bin folder to PATH).

macOS

Homebrew makes it easy to install ExcelCompare:

$ brew update
$ brew install excel-compare

All what you have yo do

  1. Create exceldiff.cmd command file anywhere with write permission and add the following to it:
    <path-to-the-excel-compare-installation-folder>/bin/excel_cmp $2 $5
    e.g.
    /usr/local/Cellar/excel-compare/0.6.1/bin/excel_cmp $2 $5

    Why arguments two and five? It’s because Git passes seven arguments to a diff command, of which the second is the path to the old file (which likely is a temporary file) and the fifth is the path to the new file. For more detailed information, have a look at the Git documentation, specifically under GIT_EXTERNAL_DIFF.

  2. Edit the .git/config file in your repository and amend the following path:
    [diff "excel"]
    command = <path-of-the-command-file-above>

    e.g.
    [diff "excel"]
    command = /usr/local/Cellar/excel-compare/0.6.1/bin/exceldiff.cmd

  3. Next, we must tell Git to associate certain files with the excel diff driver. To do so, edit .gitattributes in the root of your repository and amend the following:
    *.xlsx diff=excel
    *.xls diff=excel

Note:

  • Git attributes are set either in a .gitattributes file in one of your directories (normally the root of your project) or in the .git/info/attributes file if you don’t want the attributes file committed with your project.
  • .gitattributes (unlike .gitignore) must be committed to have any effect, so add it to a commit at this point.

Usage

  • Compare with HEAD revision
    git diff <file_name>
  • Compare file between 2 commits
    git diff <left_commit_id> <right_commit_id> <file_name>
@mcculley
Copy link

Where you describe how to get git to recognize the .xlsx and .xls file extensions, I had to use *.xlsx diff=excel instead of .xlsdiff excel and do similar for .xls.

@PrabhatKJena
Copy link
Author

Thank you @mcculley and @stefjoosten for the feedback. I will update the instruction.

@treimers
Copy link

treimers commented May 16, 2023

I am running into a problem, the process stops with

Excel files dir1/ExcelDocument.xls and /var/folders/k1/wjz1y8rx2h935nycm6krxdsh0000gn/T/KBeZ0Y_ExcelDocument.xls differ
fatal: external diff died, stopping at dir1/ExcelDocument.xls

Do you have any idea how to debug or fix it?

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