Sub topic 6
Blobs: file contents without names
This chapter assumes you know basic Git (init, add, commit), can use a Unix-style shell, and know that a hash function turns input into a fixed-size fingerprint. It also assumes you have met git hash-object and git cat-file in the previous chapters.
What a blob holds
A blob is the object type that stores the raw bytes of one file. It holds no file name, no path, no mode and no timestamp. Those details live in tree objects, which you will meet in the next chapter.
Because a blob has no name, two files with identical content share one blob, even if they sit in different directories or have different names.
How the blob ID is computed
Git does not hash the bare content. It hashes a header followed by the content:
blob <size in bytes>\0<content>The header is the word blob, a space, the content length in decimal, and a NUL byte. Git hashes that whole string (SHA-1 by default) to get the object ID. Then it compresses the string with zlib and writes it to .git/objects/.
You can reproduce the ID without Git's help:
git init blob-demo
cd blob-demo
printf 'hello\n' | git hash-object --stdin
printf 'blob 6\0hello\n' | sha1sumBoth commands print ce013625030ba8dba906f756967f9e9ca394464a. The content hello\n is six bytes, which is why the header says blob 6. If you use a repository with SHA-256 object format, the sha1sum check will not match.
Writing and reading a blob
hash-object without -w only computes the ID. With -w it also stores the object.
id=$(echo 'hello' | git hash-object -w --stdin)
echo "$id"
git cat-file -t "$id"
git cat-file -s "$id"
git cat-file -p "$id"
find .git/objects -type fThe first cat-file call prints blob. The second prints 6. The third prints hello. The find command shows one file. Its directory is the first two hex digits of the ID and its file name is the remaining digits, so ce/013625030ba8dba906f756967f9e9ca394464a.
Same content, same blob
Run the write again with identical input:
echo 'hello' | git hash-object -w --stdin
find .git/objects -type fThe ID is the same and find still lists one file. Git sees that the object already exists and does nothing. This is deduplication, and it follows directly from naming objects by content.
Changing one byte gives a completely different ID and a second, separate blob. Git does not store a diff at this level. A small edit to a large file creates a full new blob. Packfiles, covered later, recover the space by storing differences between similar objects.
Common errors
- Adding a newline by accident.
echo 'hello'writeshello\n, butprintf 'hello'writes onlyhello. These produce different blobs. Check withgit cat-file -swhen an ID does not match what you expected. - Forgetting
-w. Without it,hash-objectprints an ID but stores nothing. A latercat-filefails withfatal: Not a valid object name. - Expecting a name.
git cat-file -pon a blob shows only content. To learn which file names point at it, look in a tree or ingit ls-files --stage. - Hashing a file with
--stdinversus a path.git hash-object file.txthashes the file's bytes.git hash-object --stdinhashes what you pipe in. Both give the same ID for the same bytes.
Exercise
You run printf 'abc' | git hash-object --stdin and then printf 'abc\n' | git hash-object --stdin. Do the two commands print the same ID? Why, and which header does each hash use?
Show answer
No, they print different IDs. The first input is 3 bytes, so Git hashes blob 3\0abc. The second is 4 bytes because of the trailing newline, so Git hashes blob 4\0abc\n. Different headers and content give different hashes. Neither command stores an object, because -w was not used.
Exercise
Two files, a.txt and docs/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 ID depends only on the content. The names are recorded in tree objects (and in the index), which map each name to the shared blob ID.