On my machine, if I open a windows command prompt, git bash prompt and powershell terminal and type:
echo "Apple Pie" | git hash-object --stdin
I get a different hash in each window. If I close all of the windows, reopen them all again and repeat the command in each again, each terminal returns the same hash that it did previously.
Shouldn't the hash be the same no matter where this command is run?
Also, running git --version
says that all of the three prompts are all using the same version of git (I only have one installed anyways).
CodePudding user response:
The data you're hashing in each case is different.
In Git Bash, you're hashing Apple Pie\n
; that is, the data is terminated with a LF, the Unix line ending. In CMD, you're hashing Apple Pie\r\n
, with a CRLF, the Windows line ending.
For PowerShell, anything you put through a pipe is turned into little-endian UTF-16 with a byte order mark and CRLF endings, so you're hashing that instead. git hash-object
works the same in every case, but the bytes you're passing to it are different.
If you want reproducible data, don't use echo
. Use printf
, which doesn't automatically add a newline. In addition, if you don't want your data to be converted, don't pass it through a pipe in PowerShell. Pipes in PowerShell always force things to its internal text format and are not binary safe, so I recommend avoiding PowerShell anytime you'd like to use a pipe.