Sub topic 3
Git as a content-addressable object store
This chapter assumes you know basic Git use (init, add, commit, log), can work in a Unix-style shell, and know that a hash function turns any input into a fixed-size fingerprint.
The core idea
Git is a key-value store. The key is a hash computed from the value, so the key is called a content address. Everything Git records is an object in this store: file contents, directory listings, commits and tags. To read an object, you ask for its hash.
This gives Git three properties:
- Identical content has one identity. Storing the same bytes twice produces the same key, so Git keeps only one copy.
- Content can't change silently. If the bytes change, the hash changes, so the object becomes a different object.
- Corruption is detectable. Git can rehash an object and compare the result with its name.
How an object's hash is computed
Git doesn't hash the raw content alone. It builds a header from the object type, a space, the content size in bytes, and a NUL byte. It then hashes the header followed by the content.
<type> <size>\0<content>The resulting SHA-1 is 40 hexadecimal characters. Repositories can also use SHA-256, which gives 64 characters. The rest of this tutorial shows SHA-1 names.
Verify it yourself
The following commands compute a blob's hash by hand and then ask Git for the same hash. The string hello plus a newline is 6 bytes, so the header is blob 6.
git init object-store-demo
cd object-store-demo
printf 'blob 6\0hello\n' | sha1sum
echo 'hello' | git hash-object --stdinBoth commands print ce013625030ba8dba906f756967f9e9ca394464a. The sha1sum output also ends with a - for standard input, which you can ignore.
Without -w, git hash-object only computes the hash. Add -w to write the object into the store:
echo 'hello' | git hash-object -w --stdin
find .git/objects -type fThe find command lists one file: .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a. The first two hex characters name a subdirectory, and the other 38 name the file. Splitting the name this way keeps any one directory from holding too many entries.
Running the -w command a second time changes nothing. Git sees that the object already exists and leaves it alone.
What the store does not contain
The blob above holds only the bytes hello\n. It has no file name, no permissions and no timestamp. Those details live in other object types, which later chapters cover:
| Object type | What it records |
|---|---|
| blob | File contents |
| tree | Names, modes and the hashes of blobs and subtrees |
| commit | A tree, parent commits, author and message |
| tag | A named, signed or annotated pointer to another object |
Objects refer to each other by hash. A commit names a tree, and a tree names blobs. Because each hash covers the content it points to, changing one file changes the blob's hash, then the tree's hash, then the commit's hash.
Common errors
- Hashing the content without the header.
echo hello | sha1sumgives a different value from Git's. Git always hashes the header too. - Forgetting the trailing newline.
echoadds one, andprintf 'hello'does not. These are different contents with different hashes. - Expecting
-wby default.git hash-objectdoesn't write anything unless you pass-w. - Assuming the hash identifies a file path. The hash identifies content only. Two files with the same bytes share one blob.
Exercise
Two files, a/notes.txt and b/copy.txt, contain exactly the same bytes. How many blob objects does Git store for them, and where is each file's name recorded?
Show answer
Git stores one blob, because the hash depends only on the header and content, which are identical. The names notes.txt and copy.txt are recorded in the tree objects for directories a and b, not in the blob.
Exercise
You run printf 'hello' | git hash-object --stdin and get a different hash from echo 'hello' | git hash-object --stdin. Why?
Show answer
echo appends a newline, so its content is 6 bytes. printf 'hello' writes 5 bytes. The headers (blob 6 and blob 5) and the content both differ, so the hashes differ.