1 · Where a commit actually lives
In one sentence:
.git/objectsis a folder of compressed files, each named by the hash of its own content, and every commit is three kinds of them, blobs, trees and commits, pointing at each other.
The problem it solves
You run git reset --hard on the wrong branch, and an afternoon of commits disappears from git log. Are they
gone? To answer that you need to know what a commit is on disk, and what git log was showing you. People who
use git every day often can’t say. This lesson answers the first half: what a commit is made of and where it
lives. Lesson 2 answers the second.
The idea: name every piece by its content
Git keeps your project’s history in one folder, .git/objects. Everything in it follows one rule:
An object’s name is the hash of its content. Git takes the bytes, puts a short header in front, runs SHA-1 (a function that turns any bytes into a fixed-size fingerprint) and uses the 40 hex characters it returns as the file name. Think of a library where every book’s shelf number is computed from its text: two identical books get the same number, and a book can’t be changed without moving to a new shelf.
Three kinds of object are enough for a whole history:
| Kind | Holds | Example from this lesson’s repo |
|---|---|---|
| blob | one file’s bytes. No name, no permissions | ce01362: hello |
| tree | one folder’s listing: mode, id and name of each entry (cat-file -p also prints the kind) | 57e9529: greeting.txt is blob ce01362 |
| commit | one root tree, its parent commits, author, time, message | c96d6e7: tree 57e9529, “First commit” |
What it looks like
In a new repository, .git/objects holds no files. Hash a file without storing it:
echo 'hello' > greeting.txt
git hash-object greeting.txt
ce013625030ba8dba906f756967f9e9ca394464a
That’s SHA-1 of the header blob 6, a zero byte, then the 6 bytes of content. Your shell gets the same answer without git:
printf 'blob 6\0hello\n' | shasum
ce013625030ba8dba906f756967f9e9ca394464a -
git add stores the blob. The first two hex characters become a directory, the other 38 the file name:
.git/objects/ce/013625030ba8dba906f756967f9e9ca394464a
git commit adds two more objects, the tree and the commit, making 3. git cat-file -p prints any object:
git cat-file -p HEAD
tree 57e9529754dc514a3ec10db2ff882018fbe1fcbf
author Ada <ada@example.com> 1767261600 +0000
committer Ada <ada@example.com> 1767261600 +0000
First commit
git cat-file -p 57e9529754dc514a3ec10db2ff882018fbe1fcbf
100644 blob ce013625030ba8dba906f756967f9e9ca394464a greeting.txt
100644 is the mode of an ordinary file. 1767261600 is the time in seconds since 1970 (here, 1 January 2026,
10:00 UTC).
Same content, same id. Copy greeting.txt to copy.txt and commit: the store grows from 3 objects to 5, not
6. The new tree lists both names against the one blob:
100644 blob ce013625030ba8dba906f756967f9e9ca394464a copy.txt
100644 blob ce013625030ba8dba906f756967f9e9ca394464a greeting.txt
Change one file, and the change climbs to the top. Write hello world into greeting.txt and commit. It gets
a new blob, so its tree gets a new id, so the commit does. copy.txt keeps pointing at the old blob:
100644 blob ce013625030ba8dba906f756967f9e9ca394464a copy.txt
100644 blob 3b18e512dba79e4c8300dd08aeb37f8e728b8dad greeting.txt
Each commit’s parent line holds the previous commit’s id, and that id covers its tree and its own parent. So
one commit id pins the whole snapshot and every commit before it. Nothing in the store is ever edited; a
change is always a new object.
Try it on your computer
Practical 0, after lesson 2, has you build these three kinds of
object yourself without git add or git commit, and predict their ids before git prints them.
Go deeper: the bytes in the file
An object file is the header and content compressed with zlib. Python’s standard library can open one:
python3 -c "import zlib,sys; sys.stdout.buffer.write(zlib.decompress(open('.git/objects/ce/013625030ba8dba906f756967f9e9ca394464a','rb').read()))" | od -c
0000000 b l o b 6 \0 h e l l o \n
0000015
13 bytes uncompressed (od counts in octal: 15 is 13), 21 on disk: for tiny files zlib’s own framing costs more
than it saves. A tree’s entries are binary (the 20 raw bytes of each id, not 40 hex characters), which is why you
read trees with git cat-file -p rather than cat.
Loose files like these are how objects start; lesson 3 covers
the packfiles that git gc moves them into. The format is in the
Git Book, Git Objects.
Go deeper: SHA-1 and SHA-256
A SHA-1 id is 160 bits (40 hex characters × 4 bits). SHA-1 is no longer considered safe against a deliberate collision (SHAttered, 2017), and git can already use SHA-256 instead, chosen when the repository is created:
git init -q --object-format=sha256 sha256demo
cd sha256demo
echo 'hello' | git hash-object --stdin
2cf8d83d9ee29543b34a87727421fdecb7e3f3a183d337639025de576db9ebb4
64 hex characters, 256 bits; blobs, trees and commits work exactly the same. In git 2.39 the option is still marked experimental, and a SHA-256 repository can’t in general share work with a SHA-1 one (git-init). Almost every repository you meet today is SHA-1; the plan for moving between the two is git’s hash-function-transition document.
Common misunderstandings
- “A commit stores the changes (a diff).” It stores a full snapshot: a root tree listing every file. Git computes diffs when you ask for them. Unchanged files cost nothing, because their blob is shared.
- “The blob knows its file name.” Names live only in trees. Two files with the same bytes are one blob.
- “
git addjust marks a file.” It writes the blob into.git/objectsright away. That’s why work yougit added but never committed can still be recovered (lesson 3).