Introduction
Those long codes such as ce013625030ba8dba906f756967f9e9ca394464a that appear in git log are not random numbers: they are fingerprints of content. Git's whole database is a content-addressed store: every object is filed under the hash of what it contains. Understanding this one idea explains why Git is fast, why its history cannot be changed silently, and why copying a large file into ten commits costs almost nothing.
How it works
Git stores four kinds of object: blobs (file contents), trees (directory listings), commits and annotated tags. They live in .git/objects.
A blob holds the bytes of one version of one file, and nothing else: not its name, not its permissions. Its object id is the SHA-1 hash of a short header followed by the content:
blob <size in bytes>\0<content>
where \0 is a zero byte. For a file containing hello and a newline, the header is blob 6\0, and the id is ce013625030ba8dba906f756967f9e9ca394464a, whoever computes it, on any computer. You can check with:
echo "hello" | git hash-object --stdin
git cat-file -p ce01362 # print the object's contents
git cat-file -t ce01362 # print its type: blob
Consequences:
- identical content is stored once: ten copies of the same file, or the same file unchanged in a thousand commits, are one blob;
- any change changes the id: even one different byte gives an unrelated hash, so content cannot be altered without the id changing;
- integrity: Git can verify any object by hashing it again.
Ids are usually shown abbreviated: ce01362 instead of all 40 digits. Any prefix works as long as no other object in the repository starts with it; Git shows at least 7 digits by default and more when needed.
Newer Git versions can use SHA-256 instead of SHA-1 for extra security, but the idea is the same.
Syntax
import hashlib
data = text.encode()
oid = hashlib.sha1(b"blob " + str(len(data)).encode() + b"\0" + data).hexdigest()
Example
import hashlib
def blob_id(text):
data = text.encode()
return hashlib.sha1(b"blob " + str(len(data)).encode() + b"\0" + data).hexdigest()
store = {}
def save(text):
oid = blob_id(text)
store[oid] = text
return oid
print("hello\\n ->", blob_id("hello\n"))
print("hello world\\n ->", blob_id("hello world\n"))
print("empty file ->", blob_id(""))
a = save("same text\n")
b = save("same text\n")
c = save("same text!\n")
print("two files with the same content share one object:", a == b)
print("one character changes the whole id:", a[:12], "vs", c[:12])
print("objects stored:", len(store))
print("read back by id:", repr(store[c]))Line by line
blob_idbuilds the header with the length in bytes, adds the content and hashes it with SHA-1, exactly like Git.- The ids for "hello", "hello world" and the empty file are the same ones
git hash-objectprints. savestores content under its id, so saving the same text twice stores one object.- Adding a single "!" gives a completely different id.
- Objects are read back by their id, which is how Git finds everything.
Expected result
hello\n -> ce013625030ba8dba906f756967f9e9ca394464a
hello world\n -> 3b18e512dba79e4c8300dd08aeb37f8e728b8dad
empty file -> e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
two files with the same content share one object: True
one character changes the whole id: 2ee89e14b1ba vs 6526e5593d1d
objects stored: 2
read back by id: 'same text!\n'
In the real world
Every Git host verifies objects by their hashes when you push. Other systems use content addressing too: Docker image layers, the IPFS file network, backup tools that store each unique chunk once, and package managers that check downloads against a hash. Git's switch from SHA-1 to SHA-256 began after researchers produced two different files with the same SHA-1 in 2017.
Common mistakes
- Expecting a blob to know its file name: names live in trees.
- Hashing the content without the header, which gives a different id from Git's.
- Counting characters instead of bytes in the header for non-ASCII text.
- Using an abbreviation that is too short, which becomes ambiguous as the repository grows.
Best practices
- Use
git cat-file -pto explore objects when you want to see what Git really stores. - Quote at least 7 to 12 characters of a hash in messages and documents.
- Treat hashes as names for content: if the hash matches, the content is exactly the same.
Summary
Git stores file contents as blobs named by the SHA-1 of blob <size>\0<content>. Identical contents share one object, any change produces a new id, and abbreviated ids are fine while they stay unique. This content addressing is the foundation of trees, commits and Git's integrity.
Quick check
Key takeaways
- A blob's id is SHA-1 of "blob", a space, the size in bytes, a zero byte, and the contents.
- The same content always gets the same id, so it is stored once, however many files or commits use it.
- Any change to the content, even one character, gives a completely different id.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.