Git Object Model & DAG

Git stores everything as objects. Each object is named by the SHA-1 hash of its content: a 40-character code calculated from that content, usually written as…

Open the .git folder in any repo and go into objects. There's no file called README.md or app.js in there. Most of what you see are folders with two-character names, holding files named with long strings of random-looking letters and digits. It looks like a mess, but this is exactly what makes Git fast and saves space.

Git stores everything (file contents, lists of files, commits) as objects. Every object is named by the hash of its content: a code calculated from that content. A hash works like a fingerprint for content. The same content always gives the same hash, and a single changed letter changes the hash completely. Since an object's name comes from its content, Git never stores the same content twice.

The hash: a name made from the content

In the exercise, the repository holds README.md (containing # Object Model) and app.js. The command git hash-object calculates the hash of a file:

$ git hash-object README.md
e6962494240ce890906939be986e7c9a196f20a0

The four kinds of object

Git has four kinds of object:

Object What it holds In this repo
blob the content of one file, without its name the content of README.md, of app.js
tree a list of file and folder names, each with the hash of its blob or tree the project folder
commit a tree hash, the parent hash, the author, the time and the message initial, update
tag a pointer to one commit, plus who made it and a message (an annotated tag) covered in the Git Tag lesson

The parent is the commit before, and the author is who made it. The command git cat-file -p prints any object. The example below uses the exercise's repo after two commits. The first commit has the message initial. Then echo added >> app.js adds the line added at the end of app.js, and the second commit has the message update. HEAD marks where you are now, so git cat-file -p HEAD opens the last commit:

$ git cat-file -p HEAD
tree f42ae588e355b1868af494346ae690c501821376
parent 24f0fbab243182bc80947f22cd64795997a2342d
author Budi <[email protected]> 1767225660 +0700
committer Budi <[email protected]> 1767225660 +0700

update
$ git cat-file -p f42ae58
100644 blob e6962494240ce890906939be986e7c9a196f20a0	README.md
100644 blob 954567f8f8a8fd5349f30d8d5d685f6eba6703af	app.js
$ git cat-file -p 954567f
console.log(1)
added

A commit stores a snapshot, not a diff

In the second commit, only app.js changed. Guess first: how many copies of README.md's content does Git store? This table shows the trees of the two commits:

Commit initial (24f0fba) Commit update (b599099)
tree 66f43bf f42ae58
README.md e696249 e696249
app.js e14c4f2 954567f

The answer is one. Every commit does store a full snapshot: a tree that names every file. But README.md's content didn't change, so its hash is the same, and both trees point at the same blob. Its content is stored once. There are 7 objects in all: 3 blobs, 2 trees, 2 commits.

git diff and git show calculate diffs when you run them, by comparing two snapshots. To save space, Git later packs objects into one file called a packfile. But to Git, each commit is still a snapshot.

Branches and HEAD are only pointers

The terminal command cat prints what a file holds. Here are two small files in .git:

$ cat .git/refs/heads/main
b599099245eff68a5dfc714169941a4885e7242c
$ cat .git/HEAD
ref: refs/heads/main

🤔 Why is the history called a DAG?

Every commit points at its parent, never at its children (the commits made after it). A merge commit (one that joins two branches) has two parents. If you draw each commit as a dot and each link to a parent as an arrow, every arrow points into the past and can never loop back. A drawing of dots and arrows like this is called a directed graph without cycles (loops): a Directed Acyclic Graph (DAG). The hash guarantees it: a commit's hash is calculated from its parent's hash, so a commit can't point at a commit made after it.

⚠️ Wrong ideas about what's inside .git

TL;DR: Git stores everything as objects. Each object is named by the SHA-1 hash of its content: a 40-character code calculated from that content, usually written as 7 characters. There are 4 kinds of object: blob (a file's content), tree (a list of file names with their hashes), commit (the tree, the parent commit that came before, the author who made it, the time and the message) and tag (an annotated tag, a label for one commit). A commit stores a snapshot of every file, but the same content is stored only once. A branch is a pointer to a commit: a small file in .git/refs/heads/ holding one hash. HEAD marks where you are: the file .git/HEAD holds a ref (a reference) to the active branch, ref: refs/heads/main. git cat-file -p prints any object.

⚡ A name made from the content

Write:

git hash-object README.md

Meaning: Git stores everything (file contents, lists of files, commits) as objects. Each object is named by the hash of its content: a 40-character code calculated from that content, usually written as the first 7. git hash-object calculates a file's hash without storing it. The hash comes from the content, not the file name, so two files with different names and exactly the same content have the same hash.

⚡ Four kinds of object

Write:

git cat-file -p HEAD

Meaning: There are four kinds of object: blob (one file's content), tree (a list of file names with their hashes), commit (the tree, the parent commit that came before, the author who made it, the time and the message) and tag (a label for one commit). A commit points to one tree, and the tree points to blobs. git cat-file -p prints any object. HEAD marks where you are now, so git cat-file -p HEAD opens the last commit.

⚡ Snapshots, but thrifty

Guess first: your repo has two commits. In the second one only app.js changed. How many times is the content of README.md stored?

Once. The content is the same, so the hash is the same, and both commits' trees point to the same blob.

Meaning: Every commit stores a full snapshot of every file, not a diff. But content that didn't change is stored only once.

⚡ A branch is only a pointer

Write:

cat .git/HEAD

Meaning: cat prints what a file holds. A branch is a small file holding the hash of one commit, not a copy of the folder. That's why making a branch in Git is cheap and fast. HEAD is kept in the file .git/HEAD. It holds a ref (a reference) to the active branch: ref: refs/heads/main.

What you will learn