Skip to content

๐Ÿ“ฆ Git Objects - Blobs, Trees & Commits โ€‹

๐Ÿงฑ The Building Blocks of Git

Everything in Git is built from three simple objects: Blobs, Trees, and Commits

Git Objects Overview

Git Object Model


๐Ÿ“บ Video Reference โ€‹

ResourceLink
๐ŸŽฌ VideoGit Objects
๐Ÿ“„ Transcript02-git-objects.txt

๐Ÿง  Mental Model: Git as a File System โ€‹

"Forget about code repositories for a moment. Think of Git as a system for maintaining snapshots of a file system."


๐Ÿ“ฆ Object 1: Blob (Binary Large Object) โ€‹

Definition โ€‹

Blob = The contents of a file, stored as a binary stream of data.

Blob vs File: Key Differences โ€‹

AspectFileBlob
ContainsContents + metadataContents only
Has name?Yes (filename)No
Has date?Yes (created, modified)No
Has permissions?YesNo
Identified byPath + nameSHA-1 hash

Visual Representation โ€‹

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                  FILE                    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚ Name: hello.txt                    โ”‚ โ”‚
โ”‚  โ”‚ Created: 2024-01-26                โ”‚ โ”‚
โ”‚  โ”‚ Modified: 2024-01-26               โ”‚ โ”‚
โ”‚  โ”‚ Permissions: 644                   โ”‚ โ”‚
โ”‚  โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€     โ”‚ โ”‚
โ”‚  โ”‚ Contents: "Hello, World!"          โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                  BLOB                    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚ Contents: "Hello, World!"          โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚  SHA-1: aaf4c61ddcc5e8a2...            โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

SHA-1 Hash Explained โ€‹

๐Ÿ”‘ Key Insight: Same content = Same hash, ALWAYS!

If you and I both create a blob with "Hello World", we'll get the exact same SHA-1 hash. This is how Git deduplicates data!

SHA-1 Format โ€‹

SHA-1 hash: 20 bytes = 40 hexadecimal characters

Example: aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
         โ†‘                                      โ†‘
         First character                   Last character
         
Git often shows short version: aaf4c61 (first 7 chars)

๐ŸŒณ Object 2: Tree โ€‹

Definition โ€‹

Tree = A directory listing that refers to blobs (files) and other trees (subdirectories).

Tree Structure โ€‹

Tree: caf7e8a...
โ”œโ”€โ”€ 100644 blob f92a0b1... peak.png
โ”œโ”€โ”€ 100644 blob 73d8ab2... 1.txt
โ””โ”€โ”€ 040000 tree 24601ac... subdirectory

Visual Representation โ€‹

File Modes in Trees โ€‹

ModeTypeDescription
100644blobRegular file
100755blobExecutable file
120000blobSymbolic link
040000treeDirectory
160000commitSubmodule

Important: Names Are in Trees, Not Blobs! โ€‹

๐Ÿ’ก Same blob, different names! The blob doesn't know its own name. Different trees can refer to the same blob with different names.

๐Ÿ“ธ Object 3: Commit โ€‹

Git Object - Commit Structure

Definition โ€‹

Commit = A snapshot of the working tree at a certain point in time, with metadata.

What a Commit Contains โ€‹

commit a1337e5...
โ”œโ”€โ”€ tree: caf7e8a...        โ† Points to root tree (snapshot)
โ”œโ”€โ”€ parent: 8b2f9c1...      โ† Previous commit (optional)
โ”œโ”€โ”€ author: John <j@x.com>  โ† Who wrote the code
โ”œโ”€โ”€ committer: Jane <j@x.com> โ† Who committed
โ”œโ”€โ”€ date: Mon Jan 26 2026   โ† When committed
โ””โ”€โ”€ message: "Initial commit" โ† Commit message

Visual Representation โ€‹

Important: Commits Store FULL Snapshots! โ€‹

๐Ÿ”‘ Key Insight: Each commit has the ENTIRE snapshot, but Git is smart!
  • Changed files โ†’ New blob created
  • Unchanged files โ†’ Just reference existing blob (no duplication!)
This is why Git is so efficient even with full snapshots.

๐Ÿ”„ How Changes Propagate โ€‹

When you change a file, watch how hashes ripple upward:

Change Propagation Rules โ€‹

What ChangedWhat Gets New Hash
File contentsBlob + all parent Trees + Commit
Directory structureTree + all parent Trees + Commit
Only commit messageJust the Commit

๐Ÿงช Hands-On Lab: Exploring Objects โ€‹

Setup โ€‹

bash
# Create a test repository
mkdir git-objects-lab && cd git-objects-lab
git init

Create and Inspect a Blob โ€‹

bash
# Create a file
echo "Hello, Git Internals!" > hello.txt

# Stage it (this creates the blob)
git add hello.txt

# Find the blob
find .git/objects -type f
# Output: .git/objects/5e/1c309dae7f45e0f39b1bf3ac3cd9db12e7d689

# Inspect the blob
git cat-file -t 5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689
# Output: blob

git cat-file -p 5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689
# Output: Hello, Git Internals!

Create and Inspect a Tree โ€‹

bash
# Commit to create a tree
git commit -m "Initial commit"

# Find the tree from the commit
git cat-file -p HEAD
# Output:
# tree 8b137891791fe96927ad78e64b0aad7bded08bdc
# author ...
# committer ...
# Initial commit

# Inspect the tree
git cat-file -p 8b137891791fe96927ad78e64b0aad7bded08bdc
# Output: 100644 blob 5e1c309... hello.txt

Visualize the Object Graph โ€‹

bash
# See all objects
find .git/objects -type f | while read f; do
    hash=$(echo $f | sed 's:.git/objects/::' | tr -d '/')
    type=$(git cat-file -t $hash)
    echo "$hash ($type)"
done

๐ŸŽฏ Quick Reference: Git Object Commands โ€‹

CommandPurposeExample
git cat-file -t <sha>Show object typegit cat-file -t abc123
git cat-file -p <sha>Pretty-print objectgit cat-file -p abc123
git cat-file -s <sha>Show object sizegit cat-file -s abc123
git hash-object <file>Compute hashgit hash-object hello.txt
git hash-object -w <file>Compute hash & write blobgit hash-object -w hello.txt

๐Ÿง  Quiz: Test Your Understanding โ€‹

1. If I create a blob with "Git is awesome" and you create the same on your computer, will we have the same hash?

โœ… YES! Same content always produces the same SHA-1 hash. This is content-addressed storage.

2. If we both create a tree pointing to that blob with the same name, same hash?

โœ… YES! Trees are also identified by their content (which includes the blobs they point to).

3. If we both create commits of that tree with message "hello", same hash?

โŒ NO! Commits include metadata like author, timestamp, and committer - these will differ!

4. Does Git store diffs between commits?

โŒ NO! Git stores full snapshots. But it's efficient because unchanged blobs are reused (not duplicated).


๐Ÿ“Š Object Relationship Summary โ€‹


๏ฟฝ What's Next? โ€‹

Git Objects Summary

๐ŸŒฟ Next: Branches (Lesson 03)

Now that you understand objects, let's learn how branches work. Spoiler: they're much simpler than you think - just a file containing a commit SHA!

Continue to Lesson 03 โ†’


๐Ÿ“ Summary โ€‹

ObjectWhat It IsIdentified ByContains
BlobFile contentsSHA-1 of contentRaw bytes
TreeDirectorySHA-1 of listingBlobs + Trees
CommitSnapshotSHA-1 of metadata + treeTree + parent + metadata
Remember: Everything in Git is an object, and every object has a SHA-1 hash. That's it! ๐ŸŽ‰

Released under the MIT License.