Sub topic 4
Writing objects with git hash-object
This chapter assumes you know basic Git (init, add, commit), can work in a Unix-style shell, and know that a hash function turns input into a fixed-size fingerprint. It also assumes you have seen the .git directory and the idea of a content-addressable store from the earlier chapters.
What the command does
git hash-object takes content, computes the object ID Git would give it, and optionally stores it in the object database. Without -w it only prints the ID. With -w it also writes the object to .git/objects.
Git does not hash your bytes alone. It hashes a header followed by the content:
blob <size in bytes>\0<content>The header holds the object type, a space, the content size in decimal, and a NUL byte. Because the type and size are part of the hash, a blob and a commit with identical bodies still get different IDs.
Hashing without storing
Start in a fresh repository and hash some text without -w:
git init plumbing-demo
cd plumbing-demo
echo 'hello' | git hash-object --stdin
find .git/objects -type fThe command prints ce013625030ba8dba906f756967f9e9ca394464a. The find command prints nothing, because no object was written.
Storing the object
Add -w and capture the ID in a shell variable:
id=$(echo 'hello' | git hash-object -w --stdin)
echo "$id"
find .git/objects -type ffind now lists .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a. Git uses the first two hex digits as a directory name and the remaining digits as the file name. This keeps any single directory from holding too many files. The file contents are zlib-compressed, so cat will show unreadable bytes. The next chapter covers git cat-file, which reads objects back.
Checking the hash yourself
You can reproduce the ID without Git. The text hello plus the newline from echo is 6 bytes:
printf 'blob 6\0hello\n' | sha1sumThe digest matches the ID above. This confirms that an object ID is a hash of the header and content, with nothing else mixed in.
Hashing a file
Pass a path instead of --stdin to hash a file:
echo 'version 1' > test.txt
git hash-object -w test.txtThe object holds only the bytes of test.txt. The file name is not stored. If you copy the file to another name, hash-object returns the same ID, and Git writes nothing new because that object already exists. This is how identical files share one blob.
Common errors
- Trailing newline.
echoadds a newline andecho -ndoes not. The two produce different content, a different size in the header, and a different ID. - Forgetting
-w. You get an ID, but nothing is stored. Later commands that look up the ID fail withfatal: Not a valid object name. - Running outside a repository. With
-w, the command needs a repository to write into. Without-w, it works anywhere. - Expecting SHA-1 everywhere. Repositories created with
git init --object-format=sha256produce longer IDs, so the hashes differ from those shown here.
Exercise
You run echo -n 'hello' | git hash-object --stdin and get a different ID from echo 'hello' | git hash-object --stdin. What header does each input produce, and why do the IDs differ?
Show answer
The first input is 5 bytes, so Git hashes blob 5\0hello. The second is 6 bytes because echo adds a newline, so Git hashes blob 6\0hello\n. The content and the size in the header both differ, so the SHA-1 digests differ.