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
- A Git hash (from a formula called SHA-1) is 40 hexadecimal characters: the digits 0 to 9 and the letters a to f. Usually the first 7 are enough:
e696249. - The hash is calculated from the file's content (plus the object's type and size), not from the file's name. Two files with different names and exactly the same content have the same hash. A
README.mdholding# Object Modelhas the hashe696249on anybody's computer. git hash-objectonly calculates the hash; it stores nothing.
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 doesn't store files directly. It points at one tree, and that tree points at the blobs. The second and third commands follow that chain: its tree, then the blob of
app.js. parentpoints at the commit before. The first commit has no parent.100644means a regular file. The file name is stored in the tree, not in the blob.- The time is written as seconds since 1 January 1970 (
1767225660), plus the time zone+0700. Because the name and the time are part of the hash, the commit hashes on your computer will differ, even with the same files.
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
- A branch is a small file holding the hash of one commit: a pointer to a commit, not a copy of the files. That's why making a branch in Git is cheap and fast.
- The file
.git/HEADholds a ref (a reference) to the active branch:ref: refs/heads/main. - When you commit, Git makes a new commit object with the old commit as its parent. Then Git writes the new commit's hash into
refs/heads/main. HEAD moves too, because HEAD points atmain. - You can also move straight to one commit, without a branch:
git switch --detach 24f0fba, or the older way,git checkout 24f0fba.HEADthen holds that commit's hash itself. That state is called a detached HEAD (a HEAD that is not attached to a branch). - After
git checkout 24f0fba, Git writesYou are in 'detached HEAD' state.Withgit switch --detach, that message doesn't appear. To leave the detached HEAD, go back to a branch withgit switch main, orgit switch -to go back to the branch you were on before. If you made commits there, first make a new branch that points at them:git switch -c branch-name. If you don't, Git warnsyou are leaving 1 commit behindwhen you move, and that commit is left with no branch pointing at it.
🤔 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
- Thinking a branch is a copy of the folder. A branch is just a 41-byte file holding a hash. The files' contents stay in the same objects.
- Editing what's inside
.gitby hand. An object's name is calculated from its content, so changing the content breaks the repo. Let Git's commands do the writing in.git. cat .git/refs/heads/mainsays the file doesn't exist. Git sometimes packs refs into one file,.git/packed-refs. The way that always works:git rev-parse main, which prints the hash thatmainpoints at.- A second commit with no change. Real Git refuses a commit that changes nothing (
nothing to commit, working tree clean). In the exercise, changeapp.jsfirst withecho added >> app.js, thengit add .and commitupdate.
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.