When we use Git, we usually interact with commands like git add, git commit, and git push.
But have you ever wondered what actually happens inside Git when you run these commands?
This article will help you understand:
What the
.gitfolder really isHow Git stores your code internally
What are Blob, Tree, and Commit objects
How Git tracks changes
How hashes ensure data integrity
The goal is not to memorize commands but to build a mental model of how Git works internally.
1. What is the .git Folder and Why Does It Exist?
When you run:
git init
Git creates a hidden folder named .git in your project directory.
This .git folder is the brain of Git.
It contains everything Git needs to track your project:
History of commits
Information about branches
Objects (files and snapshots)
Configuration
References (HEAD, branches, tags)
Without the .git folder, your project is just a normal folder.
With .git, it becomes a Git repository.
So we can say:
Your entire Git repository lives inside the
.gitfolder.
2. Structure of the .git Directory (Conceptual)
Inside .git, you will find folders like:
objects/→ stores all data (files and commits)refs/→ stores branch and tag referencesHEAD→ points to the current branchindex→ staging areaconfig→ repository configurationlogs/→ history of changes
You normally never edit these manually, but understanding them helps you understand Git deeply.
3. Git Objects: Blob, Tree, Commit
Git stores everything as objects.
There are three main types of objects:
1. Blob (Binary Large Object)
A blob stores the content of a file.
It does NOT store filename
It only stores file data
Example: content of
index.html
So if two files have the same content, Git stores only one blob.
2. Tree
A tree represents a directory.
It stores:
File names
Folder structure
References to blobs and other trees
Think of it like:
A tree = folder snapshot
3. Commit
A commit stores:
Reference to a tree (project snapshot)
Author info
Message
Parent commit (previous history)
So a commit connects everything:
Commit → Tree → Blobs
This is how Git builds a chain of history.
4. How Git Tracks Changes
Git does NOT track changes line-by-line like traditional systems.
Instead, Git stores snapshots.
Each commit is a snapshot of your entire project at that time.
If a file does not change:
Git reuses the same blob object
No duplication happens
This makes Git:
Fast
Memory efficient
Reliable
5. What Happens Internally During git add?
When you run:
git add file.txt
Internally Git does the following:
Reads the content of
file.txtCreates a blob object
Generates a hash for that content
Stores it inside
.git/objectsAdds reference to the staging area (
indexfile)
So:
git add= move changes from working directory to staging area
It does NOT create a commit yet.
It just prepares data.
6. What Happens Internally During git commit?
When you run:
git commit -m "My first commit"
Git performs these steps:
Reads staged blobs from index
Creates a tree object (directory structure)
Creates a commit object
Stores commit inside
.git/objectsUpdates branch reference
Moves HEAD to new commit
So:
git commit= create a permanent snapshot in history
7. How Git Uses Hashes to Ensure Integrity
Git uses SHA-1 hash (or SHA-256 in newer versions).
Every object (blob, tree, commit) is identified by a hash like:
a3f5c2e9d4...
This hash depends on:
Object content
Object type
Metadata
If even one character changes, the hash changes completely.
This gives Git:
Data integrity
Tamper detection
Unique identification
So Git can always verify:
"Has this data been modified?"
8. Relationship Between Commits, Trees, and Blobs
Conceptually:
Blob → file content
Tree → folder structure
Commit → snapshot + metadata
Flow looks like:
Commit
↓
Tree
↓
Blobs
Each commit points to a tree, and each tree points to blobs.
9. Mental Model of Git (Simple)
Instead of thinking:
Git = commands
Think like this:
.git= databaseBlob = file data
Tree = folder snapshot
Commit = project snapshot
Hash = ID card
Branch = pointer to commit
HEAD = current pointer
Git is basically a content-addressable file system with history.
10. Internal Flow of git add and git commit
git add flow:
Working Directory → Staging Area (index) → Objects (blob)
git commit flow:
Staging Area → Tree → Commit → Branch → HEAD update
This explains why Git has three areas:
Working directory
Staging area
Repository
Conclusion
Understanding Git internally helps you:
Debug problems better
Use Git confidently
Avoid memorizing commands blindly
Build a strong mental model
The .git folder is not magic.
It is just a well-organized database of objects connected by hashes.
Once you understand:
Blob
Tree
Commit
Hash
Staging area
Git becomes logical instead of confusing.