How Git Works Internally: Build a Mental Model Instead of Memorizing Commands

- What is the
.gitFolder and Why It Exists
When you run:
git init
Git creates a .git directory.
This folder is the entire Git repository. Everything Git knows about your project lives here.
Your working directory = files you see.git directory = Git’s brain
If you delete .git, your project becomes a normal folder.
Basic structure:
.git/
├── objects/
├── refs/
├── HEAD
├── index
├── config
Meaning:
objects/→ where all data is stored (commits, files, trees)refs/→ branch and tag pointersHEAD→ tells you which branch you’re onindex→ staging areaconfig→ repo-level configuration

]
- Git Objects: Blob, Tree, Commit
Everything in Git is an object. There are only three main ones you need:
| Object | Represents |
| Blob | File content |
| Tree | Directory structure |
| Commit | Snapshot + metadata |
Git does not store “files”.
It stores content.
Example mental model:
Commit
↓
Tree
↓
Blobs (file contents)
Blob:
Stores raw file data
No filename
No path
Tree:
Maps filenames → blobs
Represents a folder
Commit:
Points to a tree
Has author, message, timestamp
Points to parent commit(s)

- How Git Tracks Changes
Git does not track “changes”.
It tracks snapshots.
Every commit is a full snapshot of your project.
But Git is smart:
If a file didn’t change, its blob is reused.
Only new content creates new objects.
So Git is efficient without using diffs internally.
Mental shift:
Git stores versions of your project, not versions of lines.
- What Happens During
git add
When you run:
git add file.txt
Internally:
Git reads the file
Creates a blob object
Hashes the content
Stores it in
.git/objectsUpdates the index (staging area)
Nothing is committed yet.
Working Directory → Staging Area

- What Happens During
git commit
When you run:
git commit -m "message"
Internally:
Git reads the index
Builds a tree object
Creates a commit object
Stores it in
.git/objectsMoves branch pointer to this commit
Staging Area → Commit → Branch updated

- How Git Uses Hashes
Git uses SHA-1 (or SHA-256 in newer versions).
Each object:
hash = SHA(content + metadata)
This gives:
Integrity: content cannot change without hash changing
Deduplication: same content → same hash
Security: corruption is detectable
That’s why:
.git/objects/ab/cdef...
Folders are split using hash prefixes.
Mental model:
Git doesn’t name files. It names content.

- Building the Real Mental Model
Forget commands. Think in layers:
Working Directory
↓
Staging Area (index)
↓
Commit (objects)
↓
Branch pointer
Commands just move data between these layers.
| Command | Meaning |
| git add | Save content into Git |
| git commit | Create a snapshot |
| git checkout | Move pointers |
| git reset | Move history references |
- Why This Model Makes You Strong in Git
Once you understand this:
Merge conflicts make sense
Rebase is not scary
Detached HEAD is logical
You can fix mistakes confidently
You stop guessing and start controlling Git.




