Skip to content
Tutorials Center
My learning

Topics

Lesson 43 of 78 ยท Git Internals

Git Objects: Blobs, Trees, Commits and Tags

Look inside Git's object database. Learn blobs, trees, commits and tags, how object IDs are computed, and build a commit by hand with hash-object and commit-tree.

Every Git command you have used so far, from add to merge, is a convenient front end for a very small idea: a database where you store a piece of content and get back a name computed from that content. That database is called the object database, and it lives in .git/objects. Once you understand its four kinds of objects, Git stops feeling magical. A commit is a small text file. A branch is a pointer. History is a chain of hashes.

This lesson opens the box. You will store content by hand, read it back, build a tree and a commit without ever running git add or git commit, and see why the same content always gets the same name. Git has two sets of commands: porcelain (the friendly ones such as commit, log, switch) and plumbing (low-level building blocks such as hash-object and cat-file). Here we use plumbing on purpose. You will rarely need these commands in daily work, but they make the model visible. The previous lessons gave you the picture in How Git Works; now we check it with real bytes.

Content-addressable storage

Git is a content-addressable store: the key under which something is stored is derived from the thing itself. You hand Git some content, Git computes a fingerprint of it with a hash function, and that fingerprint becomes the object's ID (also called its name, hash, or SHA).

The fingerprint is not computed from the raw content alone. Git first prepends a small header, <type> <size> followed by a NUL byte, and hashes header plus content. For a file with the text hello and a newline (6 bytes), the stored bytes are blob 6\0hello\n. The default hash is SHA-1, which gives a 40-character hexadecimal ID. Because the ID depends only on the content, two consequences follow:

  • Identical content is stored once. Ten files with the same text, in ten repositories, share one ID.
  • Content cannot change silently. If one byte changes, the ID changes. Anything that refers to an ID, such as a tree or a commit, refers to exactly that content.

Compress the header-plus-content with zlib, store it in a file named after the ID, and you have a Git object. The first two hex characters become a folder name and the other 38 the file name: .git/objects/ce/013625....

Blobs: the contents of a file

A blob (binary large object) holds the contents of one file and nothing else. No file name, no permissions, no dates. git hash-object computes the ID, and with -w it also writes the object. git cat-file reads objects back: -t shows the type, -s the size and -p pretty-prints the content.

Bash Store content and read it back

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.

The ID of hello plus a newline is ce013625030ba8dba906f756967f9e9ca394464a on every machine in the world, in every repository, forever. That is the point of content addressing. The path shown by find is the ID cut after two characters. You can also check the header rule yourself: the next example hashes the same bytes with sha1sum, adding the blob 6 header and the NUL byte by hand.

Bash Compute a Git ID by hand with sha1sum

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.

Both hashes match, so there is no secret: an ID is SHA-1 of "blob <size>\0<content>". The last part shows deduplication. After staging two files with identical text, ls-files --stage lists two paths with the same blob ID, and count-objects reports a single new loose object. The file name is not part of the blob. Names belong to trees.

Trees: directories

A tree object records one directory: a list of entries, each with a mode, a type, an ID and a name. The mode says what the entry is (100644 normal file, 100755 executable, 120000 symlink, 040000 subdirectory, 160000 a submodule commit). An entry points to a blob (a file) or to another tree (a subdirectory). A project is a tree of trees.

TEXT
tree  (the root of the project)
 |-- 100644 blob  3b18e51...  README.md
 |-- 100644 blob  9daeafb...  main.py
 `-- 040000 tree  a1c7e02...  src
                   |-- 100644 blob  ce01362...  util.py
                   `-- 100644 blob  9daeafb...  helper.py   (same blob as main.py)

The tree gets its ID from the hash of its entries, so its ID changes when any file below it changes, and only then. This is why Git can compare two huge snapshots quickly: if a subtree has the same ID in both, nothing inside it differs and Git skips it entirely.

You normally create trees with git add (which fills the index) followed by git commit. The plumbing way is git update-index --add --cacheinfo mode,id,path to put an entry into the index, then git write-tree to turn the index into tree objects. git ls-tree lists a tree.

Bash Build a tree by hand from blobs

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.

The root tree has two entries: the blob README.md and the tree src. write-tree created the src tree automatically from the path src/app.py. Notice that we never created a file on disk. The working tree is empty; the snapshot exists purely inside .git.

Commits: snapshots with context

A commit object is tiny. It names one tree (the complete snapshot), zero or more parents (the previous commits), an author, a committer, and a message. Nothing else. There are no file changes inside a commit: diffs are computed on demand by comparing the trees of a commit and its parent.

git commit-tree <tree> -p <parent> -m "message" builds a commit from a tree. It prints the new ID but does not move any branch. For that, git update-ref moves a ref to the ID. Below we create two commits by hand, then read them with cat-file -p. The author and committer variables fix the identity and dates, so every ID in the output is reproducible.

Bash Create two commits without git add or git commit

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.

Read the raw commit: a tree line, a parent line (the first commit has none), author and committer lines with a Unix timestamp and a time zone, an empty line, then the message. Before update-ref, git log fails, because a repository has no branch that holds the commits yet; they exist in the database but nothing refers to them. After we point main at the second commit, the usual history appears. This is exactly what git commit does for you in one step: write blobs, write trees, write a commit whose parent is the current HEAD, and move the current branch.

A commit's ID covers its tree, its parent IDs, its author, date and message. Change any of them (even the commit message) and the ID changes. Because a parent's ID is part of the child, changing an old commit changes every commit after it. This is why rewriting history creates new commits, and why a branch history can be verified just by checking its tip hash.

Annotated tags: the fourth object

The fourth type is the tag object. An annotated tag (git tag -a) stores a pointer to another object, usually a commit, plus a tagger, date and message. A lightweight tag is not an object at all, just a ref file. We cover tags in Tags and Releases; here we only peek at the object.

Bash An annotated tag is an object

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.

light-tag resolves straight to a commit, so its type is commit. v1.0 resolves to a separate tag object that contains object <id>, type commit, the tag name, the tagger and the message. The syntax v1.0^{commit} means "follow the tag until you reach a commit".

Seeing everything with cat-file

git cat-file --batch-all-objects --batch-check lists every object in the repository with its type and size. It is a good way to see what a normal workflow creates: after one commit of two files, there are two blobs, one tree and one commit.

Bash Count the objects a normal commit creates

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.

The second commit adds only three objects: one new blob (the edited file), one new root tree (because a path in it changed) and one new commit. The unchanged one.txt blob is reused. This is why Git snapshots are cheap although every commit is conceptually a full copy of the project.

SHA-1, SHA-256 and collisions

SHA-1 is no longer considered safe against deliberately constructed collisions (two different inputs with one hash). Git has used a hardened SHA-1 implementation that detects known attack patterns since 2017, which keeps it safe in practice. A transition to SHA-256 is built in: git init --object-format=sha256 creates a repository whose IDs have 64 hex characters. Such a repository cannot exchange objects with a SHA-1 repository, and hosting support is still limited, so most projects stay on SHA-1 for now.

Bash A SHA-256 repository

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.

Same content, different algorithm, different name. Everything else in this lesson is identical in both formats.

You do not have to type 40 characters. Git accepts any unique prefix of an ID of at least four characters (git show ce01362), and git log --abbrev-commit or --oneline prints shortened IDs. If a prefix is ambiguous Git tells you and lists the candidates.

Why this design matters

  • Integrity. Every object is verified by its ID. git fsck can recompute every hash and detect corruption.
  • Cheap branches and merges. Branches only point to commits, and commits share unchanged trees and blobs.
  • Fast comparison. Equal IDs mean equal content, so diffs skip whole subtrees.
  • Distribution. Two repositories can find out what to exchange just by comparing IDs. Transport is the subject of Transfer Protocols.

Objects are immutable: Git never edits one, it only adds new ones. What changes over time are the references that point at them, which is the topic of the next lesson. Later, packfiles show how thousands of these small files are compressed into a handful of large ones.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is stored in a blob object?

    A blob holds raw file contents. Names and modes live in the tree that points to the blob, and diffs are computed when needed.
  2. 2. Two files in different folders contain exactly the same text. How many blobs does Git store for them?

    Object IDs are computed from content, so identical content produces the same ID and one stored object. Both tree entries point to it.
  3. 3. Which pieces of information does a commit object contain?

    A commit points to a single tree (the whole snapshot) and its parents, plus metadata. Branch names are not stored in commits.
  4. 4. You run git commit-tree on a tree and get a new commit ID. Why does git log not show it yet?

    commit-tree only writes the object. History is reached through refs, so a ref such as refs/heads/main has to be pointed at the new commit.
  5. 5. What happens to the ID of a commit if you change only its message?

    The message is part of the hashed content, so the commit gets a new ID. Children record their parent's ID, so they must be rewritten too.

Summary

  • Git is a content-addressable object store in .git/objects; an object's ID is the hash of "<type> <size>\0<content>".
  • Four object types: blob (file contents), tree (a directory listing of modes, names and IDs), commit (tree + parents + author/committer + message) and tag (annotated tag pointing to an object).
  • Identical content is stored once, and any change produces a new ID, so history is tamper-evident.
  • hash-object -w, update-index --cacheinfo, write-tree, commit-tree and update-ref are the plumbing steps behind add and commit.
  • cat-file -t/-s/-p and ls-tree let you inspect any object.
  • Objects never change; refs (branches, tags, HEAD) move and give commits their names.
  • SHA-256 repositories exist (git init --object-format=sha256), but they are not interoperable with SHA-1 ones.

Next: References, HEAD and the .git Directory