Git Reference ยท Objects, Refs and Internals
The Git object model
How Git stores history: four immutable object types (blob, tree, commit, tag), how they link by hash, and why renames, copies and branches are cheap.
Under every Git command sits a small database of immutable objects. There are exactly four kinds: blobs hold file contents, trees hold directory listings, commits point to a tree and to their parents, and annotated tags wrap another object with a name and a message. Each object is stored under the hash of its own contents, so an object can never change, and two objects with the same content are one object.
Everything else is built from these: branches and lightweight tags are just files containing an object ID, the index is a sorted list of tree entries waiting to become a tree, and a merge is a commit with two parents. Once you can read the four formats, no Git behavior is mysterious.
commit โโโถ tree (root) โโโถ blob README.md โ โโโโโโโโโโถ tree src โโโถ blob app.py โโโโถ parent commit ...
The four object types
- Blob. The bytes of one file, with no name, mode or timestamp. See Blob objects.
- Tree. A list of entries, each with a mode, a name and the ID of a blob or another tree. A tree is a directory snapshot. See Tree objects.
- Commit. The ID of one root tree, zero or more parent commits, author and committer lines, and a message. See Commit objects.
- Tag. The ID of any object, its type, a tag name, a tagger line and a message, optionally signed. See Tag objects.
Names live in trees, not in blobs. That is why renaming a file creates a new tree but no new blob, and why Git "detects" renames later by comparing content instead of storing them.
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.
How objects link
Objects refer to each other only by ID, and an ID is computed from the content, including the IDs it mentions. That makes the graph a Merkle structure: changing one byte of one file changes its blob ID, then every tree up to the root, then the commit, then every later commit. Two repositories with the same top commit ID have identical history, byte for byte.
Consequences you can rely on:
- Snapshots, not deltas. Every commit names a complete tree. Packfiles later compress similar objects with deltas, but that is storage, not the model.
- Sharing is automatic. Unchanged files and unchanged directories reuse the same objects across thousands of commits.
- Immutability. "Amending" or "rebasing" never edits an object; it creates new commits and moves a branch pointer. The old ones stay until garbage collection.
- Integrity.
git fsckcan verify every link, and signed commits or tags vouch for a whole history through one ID. - Cheap branches. A branch is a 41-byte file naming one commit; creating one writes no objects.
What is not an object
Branches, tags (the lightweight kind), HEAD, the stash and the reflog are references stored outside the object database, as covered in Refs and ref storage. The staging area is a separate file described in Index file format. File names, permissions beyond the executable bit, empty directories and timestamps are not stored in blobs; Git records only the modes 100644, 100755, 120000 (symlink), 040000 (tree) and 160000 (submodule commit) in tree entries.
Usage notes
- See any object:
git cat-file -t <id>(type),-s(size),-p(pretty-printed content). - List everything reachable:
git rev-list --objects HEAD. - Loose versus packed: new objects are stored one per file (see Loose object format);
git gcand pushes pack them (see Packfile format). - IDs are SHA-1 by default, with SHA-256 available for new repositories (Object IDs and SHA-256).
- Empty trees are real objects too; Git knows the ID of the empty tree without storing it.