Search code examples
gitobjecthashblobline-endings

Result of 'git hash-object' for a file different when using core.autocrlf=true?


I am using git log --find-object to identify commits by providing git file blobs (file content hashes).

This works fine, I get the blob before for a file by using git hash-object

However, when I issue git hash-object for the very same file, and I have set core.autocrlf=true, I get a different blob (hash value).

Accordingly git log --find-object does not identify a commit that corresponds to that 'new' blob.

What happens here? Does this mean that git hash-object does not 'work' when core.autocrlf=true?


Solution

  • When writing a file to the database, the core.autocrlf tells git to run a filter on all input files to convert CRLF line endings to LF. It has to do this before calculating the hash of the blob, because every byte you change affects the hash, by definition.

    Since git hash-object is the plumbing command used to do this, it too has to perform these filters. If you want to suppress that, and find the hash of the file if no filters were run, you can use the --no-filters option. Manual summary:

    --no-filters

    Hash the contents as is, ignoring any input filter that would have been chosen by the attributes mechanism, including the end-of-line conversion. If the file is read from standard input then this is always implied, unless the --path option is given.