3

Hy,

Just out of curiosity, I tested SHA1 generated by GIT for an image file "tech01.jpg" however the SHA1 generated by GIT and those by other tools differ.

As far as I understand, SHA1 for the same file, binary content or same text should be same irrespective of the system.

So why does the SHA1 differ generated by GIT than those by other tools. Does GIT uses different encryption algorithm or any other way that modifies the file/text or it just may be I'm missing something in my understanding of GIT usage of SHA1.

This is what I get:

Bash vs other tools SHA1 difference

I am currently using git version 2.13.0.windows.1 on via Bash (MingWWindows 7 64-bit machine if that matters.

Test Image file. Image used in testing

dkjain
  • 831
  • 1
  • 9
  • 36
  • Are you sure this is supposed to be SHA1 of the contents? [The manual](https://git-scm.com/docs/git-ls-files) doesn't say that. – orhtej2 Mar 05 '18 at 12:23
  • 1
    Please check this post with answer for your question: https://stackoverflow.com/a/24283352/8950081 – Joik Mar 05 '18 at 12:53
  • Possible duplicate of [Git - finding the SHA1 of an individual file in the index](https://stackoverflow.com/questions/460297/git-finding-the-sha1-of-an-individual-file-in-the-index) – phd Mar 05 '18 at 13:40

1 Answers1

5

Git isn't calculating the SHA-1 of the file. Each git object, including each file stored in git, has a header that includes information about the object, including the type of object (in this case, a file is a "blob" object) and the size of the object.

You can calculate Git object ID for a file by running:

git hash-object tech02.jpg

This will calculate the SHA-1 of the header followed by the contents of the file.

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
  • Actually I did go through that chapter but forgot about the header+SHA-1 bit. Thanks for your reply. – dkjain Mar 05 '18 at 13:32