Sub topic 2
Content-addressable storage and object IDs
This chapter assumes you know basic Git (init, add, commit, log), can use a Unix-style shell, and know what a cryptographic hash function is. The previous chapter showed that Git keeps its data in .git/objects. Here you will see how each object gets its name.
The working model
Git is a content-addressable store. You give it content, and it returns a key computed from that content. The key is the object ID, a hash of the object's bytes. Git uses the ID to name the object, to find it again, and to detect corruption.
Two properties follow from this:
- The same content always produces the same ID, in any repository.
- Changing even one byte produces a different ID.
Because of the first property, Git stores identical content once. Two files with the same bytes share one object, even if they have different names or live in different directories.
Computing an ID
The command git hash-object computes the ID of some content. Without -w it only prints the ID. With -w it also writes the object into the database.
git init demo && cd demo
echo 'hello' | git hash-object --stdin
echo 'hello' | git hash-object -w --stdin
find .git/objects -type fBoth hash-object commands print the same ID. Only the second one creates a file. The find command then lists one file under .git/objects.
The path of that file comes from the ID. Git uses the first two hex characters as a directory name and the remaining characters as the file name. An ID that starts with ce is stored at .git/objects/ce/.... Splitting by prefix keeps any single directory from growing too large.
The ID is a hash of a header plus the content
Git does not hash the raw content alone. It hashes a header, then a NUL byte (a zero byte), then the content. For a blob the header is the word blob, a space, and the content size in bytes.
The text hello followed by a newline is 6 bytes, so the hashed bytes are blob 6, a NUL, and hello with its newline. You can compute the ID by hand and compare:
printf 'blob 6\0hello\n' | sha1sum
echo 'hello' | git hash-object --stdinIn a repository that uses SHA-1, both commands print the same 40-character ID. Only the sha1sum output has a trailing - marker, which is not part of the ID.
The header includes the object type, which means a blob and a commit with identical bytes still get different IDs. The size in the header also ties the ID to the exact length of the content.
Common errors
- Forgetting the newline.
echo 'hello'adds a newline, so the size is 6. Withprintf 'hello'the size is 5, and the ID changes. A hand computation that does not match is usually a size or newline mistake. - Writing
\0where the shell does not expand it. Useprintf, as above, rather thanecho, becauseechohandling of escapes varies between shells. - Expecting
hash-objectto store anything. Without-w, nothing is written. Ifcat-filelater reports that an object is missing, check whether you used-w. - Assuming the ID depends on the file name. It does not. The name belongs to a tree, which a later chapter covers.
Why this design matters
Since the ID is derived from the content, an object cannot change without its ID changing. Git can verify any object by rehashing it and comparing. Two repositories can also compare IDs to decide which objects one of them lacks, without comparing the contents.
Exercise
You run printf 'hi' | git hash-object --stdin and then echo 'hi' | git hash-object --stdin. Why do the IDs differ, and what header does Git hash in each case?
Show answer
The first input is 2 bytes and the second is 3 bytes, because echo adds a newline. The hashed bytes are blob 2, a NUL, and hi in the first case, and blob 3, a NUL, and hi plus a newline in the second. Different hashed bytes give different IDs.