29
10
In order to diff binary files in git, I assume I need to set up a difftool.
What difftools work? How do you put the parameters in?
29
10
In order to diff binary files in git, I assume I need to set up a difftool.
What difftools work? How do you put the parameters in?
22
You can set a textconv config option for a filetype. See "Performing text diffs of binary files" in gitattributes(5). What you should use depends on the filetype.
Example 1:
Say you want to diff the contents of zip files. In that case you should put the following in either $GIT_DIR/config file or $HOME/.gitconfig.
[diff "zip"]
textconv = unzip -v
Next time you ask for a diff on a zip file in a repo, it will call unzip -v on both version and diff the resulting text.
Example 2:
For pdf files you could use e.g. pdfinfo;
[diff "pdf"]
textconv = pdfinfo
Example 3:
If there is no specific infomation utility for a filetype, you could e.g. use hexdump (comes with FreeBSD and OSX, also available on Linux):
[diff "bin"]
textconv = hexdump -v -C
6I finally got this to work, after adding *.bin diff=bin to my .gitattributes – Justin Rowe – 2015-01-07T13:09:13.627
For pdf, get both the header and text changes:
[diff "pdf"]
textconv="pdfinfo \"$1\";pdftotext \"$1\" - #"
cachetextconv = true – Tom Hale – 2016-09-17T07:32:46.023
I've tried to get this working for MS .xlsm files, which are just zip files, and cannot get it to work. Doing unzip -v lists the content as expected, so not sure why it's not working. Git just continues to state Binary files a and b differ. – nilskp – 2016-10-14T15:35:01.530
@nilskp You need to both define a diff in your .gitconfig file and assign that attribute to a path in .gitattributes. – Roland Smith – 2016-11-20T10:53:01.473
@RolandSmith, yeah, I did. – nilskp – 2016-11-21T19:35:43.680
@nilskp Maybe you should post what you did as a question. – Roland Smith – 2016-11-21T23:46:48.333
@RolandSmith, I did exactly as described here. But I don't have this need right now, and not enough time to formulate it into a question. – nilskp – 2016-11-22T14:19:52.493
I could diff it in hex. I'd be happy enough just knowing how many bytes are different, or at what positions the bytes differ. I ended up using Hex Fiend by cloning my git repository so I could check out both versions of the file, because I couldn't figure out how to get git to launch the program. – Nick Retallack – 2014-01-25T06:09:50.037
@NickRetallack: see added examples. – Roland Smith – 2014-01-25T08:22:58.180
2I added Example 3 to my git config, but when I do "git diff" it still just gives me the same short message: "Binary files a/file and b/file differ" – Nick Retallack – 2014-01-31T18:25:48.017
@NickRetallack Example 3 only works for files with the bin extension. AFAIK, you'll have to set up a textconv for each individual file type. – Roland Smith – 2014-02-01T17:34:46.973
What do I do if there's no extension? Can I get it to work with libmagic instead? – Nick Retallack – 2014-02-03T03:58:02.653
1If you want to use libmagic, you'll have to look into the git source code to see if that works... – Roland Smith – 2014-02-08T20:50:46.483
11
The answer from Roland Smith was helpful but is currently incomplete (see the comments) - there are two parts to this.
You can define a new diff commands in your repository's .git/config file or your personal global ~/.gitconfig file, for example a hex diff command using hexdump:
[diff "hex"]
textconv = hexdump -v -C
binary = true
Next, you need to use the repository's .gitattributes file to tell git which files should be used with this special diff command:
# Binary files (no line-ending conversions), diff using hexdump
*.bin binary diff=hex
Like the .gitignore file, the .gitattributes file should be checked into your repository.
In my case I have several different file extensions which I want to treat as binary (e.g. avoid any line ending conversions if using git on Windows), and also see any differences via hexdump:
https://github.com/peterjc/galaxy_blast/commit/5ec4695e6c3da3926fb100ca006f0f3e88c53c3d
See also https://github.com/resin-io/etcher/pull/1367 for another example defining a hexdump diff command for use with image files.
You can set the .gitattributes globally as well (to go along with the [diff] entries in your global .gitconfig). If you make the .gitattributes local to the repo, then the user will have to modify his local .gitconfig repo settings because for security reasons those will not be pushed to the remote. Either way, each user has to update their local files/config somehow to enable this behavior. In .gitconfig under [core] add attributesfile = c:/users/<username>/.gitattributes or wherever you want to store it if you make it global (note the forwardslashes, even in windows). – LightCC – 2019-11-01T20:40:46.550
11
If you want to force git to show binary files diff as a plain text diff use --text option like so:
git diff --text
-1
The above are comprehensive ways to do so.. however, if you just need to do it for a few files, the following method is what I use:
git checkout HEAD -- /path/to/file > ~/file
vimdiff ~/file /path/to/file
Here I am using vimdiff but you can use any other tool. The above can be also combined into a small script if you need to do this over and over again.
This appears to have discarded the changes to my file (and created an empty file where I wanted the committed version to be). – Erhannis – 2018-12-06T02:49:10.960
What kind of output are you expecting to get from a diff tool of a binary file? What kind of binary file is this? Is it something that can be rendered to a text format and then compared? – Zoredache – 2014-01-24T01:04:45.307