Sub topic 5
Blobs: file contents without names
This chapter assumes you know basic Git use (init, add, commit), can work in a Unix-style shell, and know what a cryptographic hash function is. Earlier chapters covered object IDs and git cat-file. Here you look at the simplest object type.
What a blob is
A blob (binary large object) is the Git object that stores the contents of one file. It holds the bytes and nothing else. It has no file name, no directory path, no permissions and no timestamp.
Those details live elsewhere. Names and modes belong to trees, which the next chapter covers. A blob is only the content, so its ID depends only on that content.
Two files, one blob
Because the ID comes from the content alone, identical files share a single blob. Try it:
git init blobdemo && cd blobdemo
echo 'hello' > a.txt
cp a.txt b.txt
git add a.txt b.txt
git ls-files -sgit ls-files -s lists the staged entries. Both lines show the same object ID, with mode 100644 and stage number 0. Only the path differs. Git stored the content once.
Reading a blob
Get the blob's ID from the index, then inspect it:
blob=$(git rev-parse :a.txt)
git cat-file -t "$blob"
git cat-file -s "$blob"
git cat-file -p "$blob"The output is:
blob, the object type.6, the size in bytes. The texthellois 5 bytes and the newline fromechois the sixth.hello, the content.
The :a.txt syntax means "the version of a.txt in the index". git rev-parse turns it into an object ID.
Changing a file creates a new blob
Blobs are never edited. If you change one byte, the hash changes, so Git writes a different blob:
echo 'hello again' > a.txt
git add a.txt
git ls-files -sThe entry for a.txt now has a new ID. b.txt still points at the old blob. The old blob stays in the object database until garbage collection removes it, and only if nothing references it.
Git stores each version as a whole blob. It does not store a diff here. Delta compression happens later, when objects are packed into packfiles, which a later chapter covers.
Common errors
- Expecting the name inside the blob. Running
git cat-file -pon a blob shows only content. To find which path uses a blob, look in a tree. - Forgetting the trailing newline.
echo 'hello'writes 6 bytes andprintf 'hello'writes 5. They produce different blobs. - Assuming
git addis the only way to create blobs.git hash-object -walso writes one, without touching the index. - Assuming line-ending changes are harmless. Converting LF to CRLF changes the bytes, so the blob and its ID change too. This is why
core.autocrlfand.gitattributessettings can make a file look modified.
What to remember
| Property | Blob |
|---|---|
| Stores | Raw file bytes |
| Does not store | Name, path, mode, timestamps |
| ID depends on | Header and content only |
| Identical content | One shared object |
| Edited content | A new object, never a change in place |
Exercise
You create docs/readme.txt and notes/copy.txt with exactly the same bytes, then run git add on both. How many blobs does Git store for them, and why? What happens to the blob count if you then add one character to copy.txt and run git add again?
Show answer
Git stores one blob, because the object ID is computed only from the header and the content, and both files have the same bytes. Their names and paths are kept in tree entries. After you add a character to copy.txt and stage it, Git writes a second blob with a new ID. readme.txt still points to the original blob.