Understanding Git Objects: Blobs, Trees, and Commits
You'll learn how Git actually stores your files and commits under the hood using a simple but powerful system.
The Big Picture
When you run git add and git commit, Git doesn't just copy your files into a hidden folder. Instead, it breaks everything down into three types of objects stored in a content-addressable database. Think of it like a library where every book has a unique catalog number based on its exact contents—if even one word changes, it gets a new number.
The Three Object Types
Blobs (Binary Large Objects): These store your actual file contents. When you stage a file with git add, Git compresses the content and creates a blob. Each blob gets a unique SHA-1 hash—a 40-character fingerprint like a1b2c3d4.... If two files have identical content, they share the same blob.
Trees: These represent directories. A tree object lists filenames and points to their corresponding blobs (or other trees for subdirectories). When you commit, Git creates a tree snapshot of your entire project structure at that moment.
Commits: These are the snapshots you create with git commit. A commit object points to a tree (your project's state), contains metadata (author, date, message), and points to its parent commit(s). This creates the chain you see with git log.
The Content-Addressable Magic
Git calculates each object's SHA-1 hash from its content. This means identical content always produces the same hash, making Git incredibly efficient at detecting duplicates and changes. It's also how Git ensures data integrity—tamper with anything, and the hash won't match.
Key Takeaway: Git stores your project as a graph of immutable objects (blobs, trees, commits) identified by SHA-1 hashes, creating an efficient, tamper-proof version history.