How Git Works Internally: Building a Mental Model đź§

Search for a command to run...

No comments yet. Be the first to comment.
DNS Explained with dig: From Root Servers to Google.com When you type google.com into your browser, a surprising amount of distributed system machinery spins up behind the scenes.At the heart of it all is DNS — the Domain Name System. This article br...

Before Git, GitHub, and modern collaboration tools, software development looked very different — and honestly, a bit chaotic. If you’ve ever seen folders named final, final_v2, or latest_final_REAL, then you already understand why version control exi...

If you’re starting your developer journey, one of the first tools you’ll hear about is Git.Git helps you track changes in your code, collaborate with others, and safely experiment without fear of losing work. This article explains what Git is, why it...

My Tech Diary
4 posts
Most developers learn Git by memorizing commands like git add and git commit.
But Git becomes much easier once you understand what’s happening under the hood.
In this article, we’ll explore:
What the .git folder really is
How Git stores data internally
Git objects: Blob, Tree, Commit
What actually happens during git add and git commit
How Git uses hashes to protect your data
No advanced math, no magic — just clear concepts.
When you understand Git internally:
You stop guessing and start reasoning
Commands make logical sense
Debugging Git issues becomes easier
You rely less on memorization
Think of this as learning how Git thinks.
.git Folder?When you run:
git init
Git creates a hidden folder called:
.git/
This folder is the heart of Git.
👉 Your project is not a Git repository because of Git commands —
it’s a repository because the .git folder exists.
Delete .git, and Git forgets everything.
.git Folder?

A simplified view:
.git/
├── objects/
├── refs/
├── HEAD
├── index
├── config
Let’s focus on the most important parts.
objects/ – Where Git Stores EverythingThis is Git’s database.
Every file
Every folder
Every commit
All are stored here as objects.
HEAD – Where You Are Right NowHEAD points to:
The current branch
The latest commit on that branch
Think of it as:
“You are here 📍”
index – The Staging AreaThe index file represents:
This is why staging exists.
Git stores data using four object types, but three are core:
Blob
Tree
Commit
Let’s break them down.
A blob stores:
File content only
No filename
No directory info
Example:
"Hello Git"
That text becomes a blob object.
💡 If two files have the same content → Git stores only one blob.
A tree represents:
A directory
Filenames
File permissions
Links to blobs or other trees
Think of a tree as:
A folder pointing to files and subfolders
A commit stores:
A reference to a tree
Parent commit(s)
Author info
Timestamp
Commit message
A commit does not store files directly.


Commit
↓
Tree
↓
Blobs (file contents)
This structure makes Git fast, efficient, and reliable.
Here’s a key idea:
đźš« Git does not primarily store diffs
âś… Git stores snapshots
Each commit:
Points to a full snapshot of the project
Reuses unchanged objects from previous commits
That’s why Git is fast and space-efficient.
git add?

When you run:
git add file.txt
Git does the following:
Reads file content
Creates a blob object
Stores it in .git/objects
Updates the index (staging area)
👉 No commit yet
👉 Just preparation
git commit?

When you run:
git commit -m "Add file"
Git:
Reads the staging area (index)
Creates a tree object
Creates a commit object
Moves HEAD to the new commit
🎉 Your snapshot is now permanent.
Every Git object is identified by a SHA-1 hash.
Example:
e83c5163316f89bfbde7d9ab23ca2e25604af290
This hash is based on:
Object content
Object type
Change the content → hash changes
Corruption is instantly detectable
History cannot be silently altered
Git is self-verifying by design.
Instead of memorizing commands, remember this:
Git stores objects
Objects are connected by hashes
Commits point to trees
Trees point to blobs
git add prepares
git commit records
Once this clicks, Git feels logical — not magical.