Search code examples
gitgit-diff

How to control hash length in git diff header


The output of git diff contains a header like so:

index f8fdb16de,78132574a..000000000

In git help diff, this is explained like this:

2. It is followed by one or more extended header lines (this example shows a merge with two parents):

       index <hash>,<hash>..<hash>
       mode <mode>,<mode>..<mode>
       new file mode <mode>
       deleted file mode <mode>,<mode>

I want to use git diff to create patches, and I want these patches to have a predictable format so they can be compared. To do this, I need a fixed length for the hash in the "index .." header.

How can I control the length of these hashes?

I tried --abbrev=7, but it seems to have no effect.

I still see my patches updated like this:

-index 52a2a35..7813257 100755
+index 52a2a357e..78132574a 100755

Solution

  • The --abbrev option only works for "diff-raw format output and diff-tree header lines". For standard patch output, you can use git diff --full-index to get a complete, unabbreviated blob ID. From the man page:

    --full-index
    Instead of the first handful of characters, show the full pre- and post-image blob object names on the "index" line when generating patch format output.

    This will produce output like:

    diff --git a/foo b/foo
    index c7bc37b70c7e29e3e4ed048c22ca3929367aa171..ab10096fde76d8c1d6172bd09d0dc4a18fb2c2fa 100644
    Binary files a/foo and b/foo differ