Git Reference ยท Objects, Refs and Internals
Blob objects
Reference for Git blob objects: how file contents are stored without names, how the ID is computed from the header, and how to create and read blobs by hand.
A blob is the simplest Git object. It stores the bytes of one file and nothing else: no file name, no permissions, no timestamps. The name and the mode of a file live in the tree that points at the blob, which is why two files with identical content in different folders share one blob.
A blob's ID is the hash of the text blob <size>, a NUL byte and then the raw content. The content is stored exactly as it was when you ran git add, after any configured line-ending or clean-filter conversion. Git never stores a blob in a "text" or "binary" form: to Git it is always a sequence of bytes.
git hash-object -w file.txt # create a blob, print its ID git cat-file -p <blob-id> # print the content git cat-file -s <blob-id> # print the size in bytes
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Format
A loose blob file holds blob, a space, the decimal size, a NUL byte and the content, all compressed with zlib (see Loose object format). Inside a packfile the blob is stored with type number 3, either whole (compressed) or as a delta against another object (see Packfile format).
| Part | Description |
|---|---|
blob |
The type name. |
<size> |
A space and the content length in bytes, in decimal. |
| NUL | A single zero byte ending the header. |
| content | The file bytes, unchanged. |
An empty file is a valid blob. In a SHA-1 repository its ID is always e69de29bb2d1d6434b8b29ae775ad8c2e48c5391, and in a SHA-256 repository it has a different, equally fixed value.
Usage notes
- Names are not part of the blob. Renaming a file does not create a new blob. Git detects renames later, by comparing blob content (see git diff options).
- Mode is not part of the blob. Making a script executable changes the tree entry (
100644to100755), not the blob. - Filters run before storage. With
core.autocrlf,.gitattributestextorfiltersettings, the stored blob can differ from the working-tree file.git hash-objectapplies the same filters unless you pass--no-filters. - Large files are stored whole. Git compresses each blob, and packfiles delta-compress similar versions, but a big binary that changes often still bloats history. Consider Git LFS for those files.
- Deleting is not possible directly. A blob stays in the database while any commit, index entry or reflog entry refers to it; only
git gcremoves unreachable blobs after the expiry period. - Symbolic links are blobs too. A symlink is stored as a blob whose content is the link target, referenced by a tree entry with mode
120000. - Submodules have no blob. A submodule is a tree entry of mode
160000that names a commit in another repository.