I'm tracking a Virtual PC virtual machine file (*.vmc) in git, and after making a change git identified the file as binary and wouldn't diff it for me. I discovered that the file was encoded in UTF-16.
Can git be taught to recognize that this file is text and handle it appropriately?
I'm using git under Cygwin, with core.autocrlf set to false. I could use mSysGit or git under UNIX, if necessary.
Solution is to filter through
cmd.exe /c "type %1"
. cmd'stype
builtin will do the conversion, and so you can use that with the textconv ability of git diff to enable text diffing of UTF-16 files (should work with UTF-8 as well, although untested).Quoting from gitattributes man page:
Performing text diffs of binary files
Sometimes it is desirable to see the diff of a text-converted version of some binary files. For example, a word processor document can be converted to an ASCII text representation, and the diff of the text shown. Even though this conversion loses some information, the resulting diff is useful for human viewing (but cannot be applied directly).
The textconv config option is used to define a program for performing such a conversion. The program should take a single argument, the name of a file to convert, and produce the resulting text on stdout.
For example, to show the diff of the exif information of a file instead of the binary information (assuming you have the exif tool installed), add the following section to your
$GIT_DIR/config
file (or$HOME/.gitconfig
file):A solution for mingw32, cygwin fans may have to alter the approach. The issue is with passing the filename to convert to cmd.exe - it will be using forward slashes, and cmd assumes backslash directory separators.
Step 1:
Create the single argument script that will do the conversion to stdout. c:\path\to\some\script.sh:
Step 2:
Set up git to be able to use the script file. Inside your git config (
~/.gitconfig
or.git/config
or seeman git-config
), put this:Step 3:
Point out files to apply this workarond to by utilizing .gitattributes files (see man gitattributes(5)):
then use
git diff
on your files.