Inside Git: How It Works and the Role of the .git Folder
Every Git repo has a dirty little secret. A hidden folder. A place where Git stores everything.
It's called .git. And today, we're breaking in.
The Secret Room
Run git init and Git creates a .git folder. This is Git's brain — delete it, and your repo forgets everything.
ls -la .git
Output:
HEAD
config
objects/
refs/
hooks/
Let's see what's inside.
Git Objects — The Building Blocks
Git stores everything as objects. Three types:
| Object | What It Stores |
|---|---|
| Blob | File contents (just data, no filename) |
| Tree | Directory structure (filenames + blob references) |
| Commit | Snapshot metadata (tree + author + message + parent) |
Every object gets a SHA-1 hash — a unique 40-character ID. Change one byte, the hash changes completely. That's how Git ensures nothing is tampered with.
What Happens When You Commit?
Step 1: git add
Git takes your file, compresses it, and stores it as a blob in .git/objects/.
git add hello.txt
Peek inside:
find .git/objects -type f
Output:
.git/objects/e9/65047ad7c7f9b8e...
That's your file — hashed and stored.
Step 2: git commit
Git creates a tree (directory snapshot) and a commit object pointing to it.
git commit -m "Add hello"
Now check the log with object details:
git log --oneline
Output:
3a7b2c1 Add hello
That 3a7b2c1? It's the commit's hash. Git's way of saying: "This exact snapshot, forever."
Inspect Any Object
Want to see what's inside an object? Use cat-file:
git cat-file -t 3a7b2c1 # Type
git cat-file -p 3a7b2c1 # Content
Output:
commit
tree 5d8f2a...
author Tanish <tanish@example.com> 1706700000 +0530
committer Tanish <tanish@example.com> 1706700000 +0530
Add hello
Git just showed you its diary.
The Mental Model
Commit → Tree → Blobs
↓
Points to parent commit (history chain)
Every commit knows its parent. That's how git log traces history. That's how branches work. That's how Git never loses anything.
Quick Peek Commands
| Command | What It Does |
|---|---|
ls .git | See Git's internal structure |
git cat-file -t <hash> | Check object type |
git cat-file -p <hash> | Print object content |
find .git/objects -type f | List all stored objects |
The .git folder isn't scary. It's just blobs, trees, and commits — all hashed, all connected.
Now you know where Git hides the bodies. 💀