Generate SHA1 hash for a file

I’m starting to learn Git the DVCS or DSCM tool. For each file that Git tracks it identifies the file (the blob) by a generated SHA-1 hash ID of the file contents.

I wanted to check that hash on a file.
First this is how you list the IDs:

C:\work\workspace\SimpleServer>git ls-tree HEAD
100644 blob 536d1f72385998faf4e4c61f4385c40ad1138b3e    .gitignore

Here is how you get the hash outside of Git:

Using sha1sum.exe

serverscygwinbinsha1sum.exe workworkspaceSimpleServer.gitignore
6062906b77fc71db2960f2e0ec83704a4d3f403e *workworkspaceSimpleServer.gitignore

Using Ant via Groovy
Here is how its done using Groovy and it’s AntBuilder DSL:

new AntBuilder().checksum (
	file: "C:workworkspaceSimpleServer.gitignore",

This creates a file,.gitignore.SHA-1, with the contents: 6062906b77fc71db2960f2e0ec83704a4d3f403e

The sha1sum.exe of cygwin and the Ant sha1 both agree, but Git’s value is different.

Ah, the Git hash of the file is of the blob of the file as Git stores it in the Git structure.

Leave a Reply

Your email address will not be published. Required fields are marked *