Sub topic 6
Blobs: file contents without names
A blob is the object Git uses to store the contents of one file. This chapter covers what a blob holds, how its ID is computed, and how to check that by hand. It assumes you can use a Unix-style shell and know that git hash-object and git cat-file write and read objects. Earlier chapters introduced both.
What a blob is
A blob (binary large object) holds the bytes of a file and nothing else. It has no filename, no directory, no permissions and no timestamp. Those details live elsewhere. A tree object, covered in the next chapter, pairs names and modes with blob IDs.
| Information | Stored in the blob? | Where it lives instead |
|---|---|---|
| File bytes | Yes | The blob |
| Size in bytes | Yes (in the header) | The blob |
| File name | No | A tree entry |
| Mode (executable or not) | No | A tree entry |
| Author and date | No | A commit |
This design has a practical consequence. Two files with identical bytes share one blob, whatever their names or directories.
The blob format and its ID
Git builds a blob's ID from a header plus the content:
blob <size in bytes>\0<content>\0 is a single NUL byte. Git hashes that whole byte sequence with SHA-1 (or SHA-256 in repositories created with --object-format=sha256). The result is the object ID. Git then compresses the same bytes with zlib and writes them to .git/objects/.
The ID depends only on the content, so you can compute it without Git. The command below creates a scratch repository and compares Git's answer with sha1sum.
git init blob-lab
cd blob-lab
echo 'hello' | git hash-object --stdin
printf 'blob 6\0hello\n' | sha1sumBoth commands print ce013625030ba8dba906f756967f9e9ca394464a. The size is 6 because echo adds a newline, so the content is hello\n: five letters plus one newline.
Writing a blob
Without -w, git hash-object only computes the ID. With -w, it also writes the object to the database. Forgetting -w is the most common mistake in this chapter's exercises.
id=$(echo 'hello' | git hash-object -w --stdin)
echo "$id"
find .git/objects -type fThe find output shows one file at .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a. The first two hex digits become a directory name, which keeps any single directory from growing too large. The other characters are the filename.
Because the object is zlib-compressed, cat shows unreadable bytes. You can still decompress it yourself to confirm the header exists.
import zlib, pathlib
p = pathlib.Path(".git/objects/ce/013625030ba8dba906f756967f9e9ca394464a")
print(zlib.decompress(p.read_bytes()))This prints b'blob 6\x00hello\n', which is exactly the byte sequence that was hashed.
Reading a blob
git cat-file reads any object by ID. For a blob, three options matter:
-tprints the type, hereblob.-sprints the content size in bytes, here6. The header is not counted.-ppretty-prints the content. For a blob this is the raw bytes, so binary files print as binary.
git cat-file -t "$id"
git cat-file -s "$id"
git cat-file -p "$id"
git cat-file blob "$id" | wc -cYou can use git cat-file blob <id> when you want the raw content and a type check in one step. Git fails if the object is not a blob. Use git cat-file -e <id> to test whether an object exists. It prints nothing and reports the answer through the exit status, which makes it useful in scripts.
Abbreviated IDs work as long as they are unambiguous, for example git cat-file -p ce0136. You can also address a blob through a commit and a path with rev:path syntax, such as git cat-file -p HEAD:hello.txt. git rev-parse HEAD:hello.txt prints the blob ID without reading the content.
Identical content, one blob
Content addressing means the same bytes always produce the same ID. This means you can copy or rename a file without making a new blob.
echo 'same text' > a.txt
cp a.txt b.txt
git hash-object a.txt b.txt
git hash-object -w a.txt b.txt
find .git/objects -type fThe first command prints the same ID twice. After the second, find lists only one new object file, even though two files were hashed. Git keeps deduplication at the object level and never needs a separate step for it.
The reverse also holds. Changing one byte of a file creates a completely different blob. Git never edits a blob in place. The old blob stays in the database until garbage collection removes it, and only if nothing refers to it. A later chapter covers that process.
Edge cases and gotchas
The trailing newline changes the ID
echo 'hello' and printf 'hello' produce different content, so they produce different IDs. When an ID does not match what you expect, check for a missing or extra newline first. Use git cat-file -s to see the size.
Files and standard input are treated differently
git hash-object <file> applies the same conversions that git add would apply. These include line-ending conversion from core.autocrlf and filters from .gitattributes. Content from --stdin is not converted. Two ways to get the same file into the database can therefore give two different IDs. Add --no-filters to hash the file exactly as it is on disk. Alternatively, add --path=<name> when using --stdin, so that Git applies the filters it would use for that path.
The empty blob
Empty content still has a header, blob 0\0, so it still has an ID.
git hash-object -t blob /dev/nullThis prints e69de29bb2d1d6434b8b29ae775ad8c2e48c5391. You will see this ID in many repositories, wherever a file is empty. For example, git add on a new empty file creates this blob.
Large files are stored whole
A loose blob holds the full file, compressed. Changing one line of a 50 MB file creates a second 50 MB (compressed) blob. Delta compression between similar blobs happens only later, when objects are packed. A later chapter covers packfiles.
Hash collisions
SHA-1 has known collision attacks. Git uses a hardened SHA-1 implementation that detects the known attack patterns and refuses to compute a colliding hash. Two different files with the same ID are not a practical concern in normal use, but the risk is why SHA-256 support exists.
Common errors
| Message | Likely cause |
|---|---|
| `fatal: Not a valid object name <id>` | The ID has a typo, or the blob was never written because `-w` was missing. |
| `fatal: git cat-file: could not get object info` | Same causes as above, when you used `-t` or `-s`. |
| `error: short object ID ... is ambiguous` | The abbreviation matches more than one object. Add more digits. |
| `fatal: git cat-file <id>: bad file` | The object file is damaged. Run `git fsck`. |
| Expected ID does not match | A trailing newline, line-ending conversion or a filter changed the bytes. |
Practice
Exercise
You run echo 'hi' | git hash-object --stdin and then git cat-file -p <that id>, and Git reports that the object is not valid. What went wrong, and what is the fix?
Show answer
Without -w, git hash-object only computes the ID and does not write the object, so the database has nothing to read. Run the command again as echo 'hi' | git hash-object -w --stdin, then use the ID it prints.
Exercise
A repository has docs/readme.txt and backup/readme-old.txt with the same bytes. How many blob objects store them, and what happens to the blob when you rename readme.txt to intro.txt?
Show answer
One blob stores both, because the ID depends only on the content. Renaming changes only the tree entry that holds the file name. The blob is unchanged, and no new blob is created.
Exercise
Write a shell command that computes the ID of the text cat followed by a newline without using Git. How many bytes follow the NUL in the hashed data?
Show answer
printf 'blob 4\0cat\n' | sha1sum. Four bytes follow the NUL: c, a, t and the newline.