The Plain Picture#
Every photo in the album gets a fingerprint computed from its exact contents. Change one pixel and the fingerprint is completely different. So the fingerprint is both the photo's name and a guarantee that nobody has altered it.
The Real Names#
A hash is a fixed-size fingerprint computed from any amount of data. The same input always gives the same hash; the tiniest change gives a completely different one; and it's practically impossible to find two inputs with the same hash. (Hashing is covered in depth in Security Fundamentals.) Git uses SHA-1, a hash that is 160 bits — 40 hex characters (How Computers Work Ch 3). Newer Git versions can also use SHA-256.
Git stores everything as objects, each named by the hash of its contents:
a blob — the contents of one file;
a tree — a folder: a list of names pointing to blobs and other trees;
a commit — points to one tree (the snapshot of the whole project), plus its parent commit(s), author, time and message.
Important: a commit is a full snapshot, not a list of differences. (Git stores it efficiently — an unchanged file is the same blob, stored once — but conceptually every commit is the complete project.)
Under the Hood — looking inside real objects#
git cat-file -p prints an object. From the practice repo:
$ git cat-file -p HEAD ← the latest commit
tree 42d8251059683e3e9e57cd838a82c9073ebd48e2
author Priya <priya@example.com> 1790569800 +0530
committer Priya <priya@example.com> 1790569800 +0530
Add quiz scoring
$ git cat-file -p HEAD^{tree} ← the tree it points to
100644 blob 62404293c572bdc747ff171fbef51cbee085156e quiz.py
$ git cat-file -p 62404293 ← the blob: the file's contents
def score(correct):
return correct * 10(1790569800 is the time as seconds since 1 January 1970, a common way computers store time. 100644 is the file mode —
recognise the 644 from The Linux Command Line Ch 4?)
commit 7ef9500 ──▶ tree 42d8251 ──▶ blob 6240429 "def score(correct): ..." "Add quiz scoring" quiz.py (no parent — first commit) later commits point back to their parent: commit 640a7d3 ──parent──▶ commit 7ef9500
Why this design is powerful:
Integrity: a commit's hash covers its tree, which covers every file, and its parent's hash — which covers the whole history before it. Change anything, anywhere in the past, and every hash after it changes. Tampering is impossible to hide.
Short IDs: you can refer to a commit by its first few hash characters (
7ef9500) as long as they're unique.
Check yourself
3 questionsWhat are Git's three main object types, and what does each hold?
Show answerHide answer
Blob = one file's contents; tree = a directory listing pointing to blobs/trees; commit = a pointer to the top tree plus parent(s), author, time, message.
Is a commit a snapshot or a diff?
Show answerHide answer
A snapshot of the whole project (stored efficiently by reusing unchanged blobs).
Why can't someone quietly change a commit from last month?
Show answerHide answer
Every commit's hash depends on its contents and its parent's hash; changing an old commit changes every later hash, which everyone would notice.