Sub topic 3
What an object looks like on disk
This chapter assumes you know basic Git use (init, add, commit, log), can work in a Unix-style shell, and understand what a cryptographic hash function does. You have already seen that Git names each object by a hash of its content. Here you will see what Git hashes, and how it stores the result in .git/objects.
The header, the NUL byte, and the content
Every object, whatever its type, has the same layout before it is stored:
<type> <size>\0<content><type>isblob,tree,commit, ortag.<size>is the length of the content in bytes, written as decimal text.\0is a single NUL byte (value zero) that ends the header.<content>is the object's data, exactly as it is.
Git calls the first part the header. The object ID is the hash of the header and the content together, not of the content alone. Because the type is part of the hashed data, a blob and a tree with identical bytes still get different IDs.
Computing an ID by hand
The text hello followed by a newline is 6 bytes, which makes its header blob 6. The next commands hash it two ways. In a repository that uses SHA-1, both print the same ID.
printf 'blob 6\0hello\n' | sha1sum
echo 'hello' | git hash-object --stdinsha1sum hashes the header and content that you built yourself. git hash-object builds the header on its own. If the sizes in your hand-built header are wrong, the two IDs will differ. A wrong size is the most common mistake when you try this.
Where the file goes
Hashing alone does not store anything. Adding -w tells Git to write the object into the object database.
git init demo && cd demo
echo 'hello' | git hash-object --stdin
echo 'hello' | git hash-object -w --stdin
find .git/objects -type fThe first command only prints the ID. The second prints the same ID and also writes a file. The find command then lists one file under .git/objects.
Git builds the file path from the ID. The first two hex characters become a directory name, and the remaining characters become the file name. For example, an ID that starts with ce013625... is stored at .git/objects/ce/013625.... Splitting by the first two characters keeps any single directory from holding too many files.
Compression
The file on disk is not the plain header and content. Git compresses the whole thing, header included, with zlib, a common compression format. The ID is computed from the uncompressed bytes, and compression happens only when writing.
This has two consequences:
- Running
caton an object file prints unreadable bytes. Usegit cat-fileto read objects, which the next chapter covers. - Compressing the file does not change the ID. Two Git versions with different compression settings still agree on every object's name.
The steps below show the full path from content to file.
Common errors
| Symptom | Cause |
|---|---|
| Your hand-computed ID does not match Git's | The size in the header is wrong, or `echo` added a newline you did not count |
| `hash-object` prints an ID but `find` shows no file | You left out `-w`, so nothing was written |
| `cat` on an object file shows garbage | The file is zlib-compressed, so read it with `git cat-file` |
| Your ID is longer than 40 characters | The repository uses SHA-256, which has a different header hash and ID length |
Exercise
You run printf 'abc' | git hash-object --stdin in a SHA-1 repository. What exact header and content would you pass to sha1sum to get the same ID?
Show answer
Pass printf 'blob 3\0abc' | sha1sum. The content abc is 3 bytes, with no trailing newline because printf does not add one, so the header is blob 3 followed by a NUL byte.
Exercise
Why do a blob and a tree that contain identical bytes get different object IDs?
Show answer
The header contains the type name, and the header is part of the hashed data. blob 6\0... and tree 6\0... hash to different values.