A file's hash can be determined without being in a git repository.
Influence of the core.autocrlf option
The following commands demonstrate the influence of the
core.autocrlf
option on calculating a file's hash.
git init --quiet repo
cd repo
Two files are created, one with the traditional Unix
end of line byte,
0x0A
, named
unix.txt
, and another one, named
dos.txt
, with the traditional Windows end of line semantics,
0x0D 0x0A
. I use the method
WriteAllBytes
of the
.NET class
System.IO.File
in order to make sure that precisly those bytes get written I intend to.
[System.IO.File]::WriteAllBytes("$pwd/unix.txt", ([byte][char] 'f', [byte][char] 'o', [byte][char] 'o', 0x0a ))
[System.IO.File]::WriteAllBytes("$pwd/dos.txt" , ([byte][char] 'f', [byte][char] 'o', [byte][char] 'o', 0x0d, 0x0a ))
git config --local core.autocrlf false
… and the files' hashes are calculated.
git hash-object unix.txt
git hash-object dos.txt
The command reports the hash of unix.txt
to be 257cc5642cb1a054f08cc83f2d943e56fd3ebe99
and the hash of dos.txt
to be e48b03ece74f47d1ae20075200c64aeaa01a9cdb
. So, these files differ, as expected.
However, with setting core.autocrlf
to true
…
git config --local core.autocrlf true
… both files will be reported to have the same hash: the hash of unix.txt
.
git hash-object unix.txt
git hash-object dos.txt