Inside Git: How its works and the role of the .git folder
When most beginners learn Git, they focus on the commands like:
git add
git commit
git push
But Git is not magic
Behind the scene Git is just files, folders and hashes working together in a very smart way.
To truly understand Git, you need to understand what happens inside the .git folder.
This article will help you to build a mental model of Git, not just memorize commands.
1. How Git works internally (Big picture)
At a high level, Git does only three main things:
- It stores snapshot of your project
- It track changes using hashes
- It connects snapshots together
Important thing to remember:
Git does not track changes line by line
Git tracks snapshots of files
Every time you commit, Git saves a snapshot of your project's state
2. Understanding the .git Folder
When you run:
git init
Git creates a hidden folder called .git/:
.git/
This folder is the heart of Git.
π Key idea
If .git folder is deleted, your project is no longer a Git repository and Git cannot track your code.
What is inside the .git folder?
Conceptually it contains:
- Git configuration
- Commit history
- Branch information
- All version of your files
You usually don't open this folder -- but understanding it makes Git click.
π Diagram: Structure of the .git Directory
Typical important folders inside .git:
.git/
βββ objects/
βββ refs/
βββ HEAD
βββ index
βββ config
Letβs understand only what matters for beginners π
3. Git Objects: Blob, Tree, Commit
Git stores everything as objects.
There are three main object types.
1οΈβ£ Blob (File Content)
- A blob store the content of a file.
- File name is not store here
- Same content = same hash
Example:
hello.txt β "Hello World"
Git store "Hello World" as a blob object.
2οΈβ£ Tree (Folder Structure)
- A tree represents a directory
- Its maps:
- file names β blob hashes
- folder names β other tree hashes
Think of a tree as:
"This folder contains these files and subfolders"
3οΈβ£ Commit (Snapshot + History)
- A commit object contains:
- Reference to tree
- Reference to the parent commit
- Author information
- Commit message
- Timestamp
A commit answers:
"What did the project look like at this moment?"
π§© Diagram: Relationship Between Commit, Tree, and Blob
This diagram helps readers visualize:
Commit
β
Tree
β
Blob (file content)
4. How Git Tracks Changes
Git does not modify files directly when you commit.
Instead of it:
- Store file content as blob
- Connects them using tree
- Saves a commit pointing to the tree
If a file does not change:
- Git reuses the old blob
- No duplicate storage
That's why Git is:
- Fast
- Efficient
- Reliable
5. What happen during git add
Let's say you edit a file:
app.py
Now run:
git add app.py
Internally Git does this:
- Reads the file content
- Create a blob object
-
Store it in:
.git/objects/ Updates the index (staging area)
Important mental model:
git adddoes not create commit
It prepares data not the next commit
6. What Happens During git commit
Now you run:
git commit -m "Add login feature"
Git now:
- Reads the staging area
- Creates a tree object
- Link to the previous commit
- Moves
HEADto the new commit
π Diagram: Internal Flow of git add and git commit
Working Directory
β git add
Staging Area (index)
β git commit
Commit (snapshot)
7.How Git Uses Hashes for Integrity
Git uses SHA-1 hashes (40 character string).
Example:
e83c5163316f89bfbde7d9ab23ca2e25604af290
Git uses hashes to:
- Identify uniquely
- Detect file corruption
- Ensure data integrity
Why this is powerful?
If:
- File content changes β hash changes
- Commit data changes β hash changes
So Git can detect tampering or corruption instantly.
This makes Git extremely trustworthy.
8. Building the Right Mental Model of Git
Instead of thinking:
β "Git is a set of commands"
Think this way:
β
"Git is a content-addressable database of snapshots"
Commands like init, add, commit, log, status, branch and checkout are just ways to interact with that database.
Final Thoughts
Understand Git internally gives you confidence.
Now you know:
- Why the
.gitfolder exists - How Git stores files
- What commits actually are
- Why Git never loses history
Once this mental model is clear:
- Git errors feel less scary
- Commands make more sense
- You stop memorizing and start understanding
Git is not magic, it's just a very well designed.π
Top comments (0)