Git Under the Hood: Working Trees, The Staging Index, Commit Trees, and Remote Tracking
A mechanical deep dive into Git internals: the four states of code, blobs, trees, commit objects, the staging index, and remote tracking branches.
On this page
Most software engineers use Git every day, yet a surprising percentage operate through trial-and-error memorization: typing git add ., git commit -m "update", and git push, and panicking when a merge conflict or detached HEAD state occurs.
Git is often perceived as an arcane, complicated CLI tool.
Underneath its user-facing commands, Git is built on an astonishingly simple, elegant computer science foundation: a content-addressable directed acyclic graph (DAG) of cryptographically hashed objects.
Trouble and confusion in Git almost never stem from complex mathematics. They occur when an engineer loses track of where code physically lives across the four internal Git storage areas.
Understanding Git means understanding how bytes move between the Working Directory, the Staging Index, the Local Repository, and the Remote Tracking Branch.
+-------------------------------------------------------------------------------+
| THE FOUR ARCHITECTURAL STAGES OF GIT |
+-------------------------------------------------------------------------------+
[1. Working Directory] [2. Staging Index] [3. Local Repository] [4. Remote]
(Raw uncommitted files) (The .git/index file) (.git/objects commit) (GitHub/GitLab)
| | | |
| --- git add filename -> | | |
| | --- git commit ---> | |
| | | --- git push ----> |
| | | |
| <====================== git checkout / restore =================== |
| <---------------------- git fetch (Updates origin/main) ---------- |
| <====================== git pull (fetch + merge) ================= |
1. The Four Code Locations
To master Git workflows, an engineer must visualize where code resides at every second:
- The Working Directory: The physical filesystem on your laptop where you edit files, run linters, and compile code. These files are unversioned until tracked.
- The Staging Area (The Index): A binary file located at
.git/index. It acts as a staging dock: a prepared snapshot of exactly what your next commit will look like. - The Local Repository (
.git/objects): A local immutable database containing permanent snapshots of your project history, organized as commit objects, tree objects, and blob objects. - The Remote Repository (
origin): A centralized server (e.g., GitHub, GitLab) holding a shared copy of the object database.
2. Git’s Internal Data Model: Blobs, Trees, and Commits
Git does not store diffs or deltas between file versions. Git stores full snapshots of files compressed as zlib objects.
Every object in .git/objects is addressed by its 40-character SHA-1 (or SHA-256) hash:
+-------------------------------------------------------------------------------+
| GIT OBJECT GRAPH ARCHITECTURE |
+-------------------------------------------------------------------------------+
Commit Object (Hash: e4f89a...)
- Tree: a1b2c3...
- Parent: 9f8e7d...
- Author: Alice <alice@example.com>
- Message: "Implement JWT middleware"
|
v
Tree Object (Hash: a1b2c3... - Represents Directory Structure)
- 100644 blob 4f9a12... package.json
- 040000 tree 8b2c41... src/
|
v
Tree Object: src/ (Hash: 8b2c41...)
- 100644 blob c3d4e5... index.ts
- 100644 blob f6a7b8... auth.ts
|
v
Blob Object (Hash: f6a7b8... - The Raw Content of auth.ts)
- Blob (Binary Large Object): Stores raw file contents. A blob holds no metadata: it does not know its file name, file path, or modification date. It only holds the file bytes.
- Tree: Represents a directory. It maps file names and permissions to child blob hashes or nested tree hashes.
- Commit: Points to a root Tree object, references parent commit hashes, and records author metadata and log messages.
3. What Common Git Commands Actually Do Under the Hood
+-------------------------------------------------------------------------------+
| COMMAND MECHANICAL IMPACT |
+-------------------------------------------------------------------------------+
Command What Physically Happens Under the Hood
---------------------------------------------------------------------------------
git add <file> Hashes file contents -> Writes a new BLOB to .git/objects ->
Updates .git/index to point file path to that new blob hash.
git commit -m "msg" Writes current index as TREE objects -> Writes a COMMIT object
pointing to root tree -> Updates branch ref (refs/heads/main)
to point to new commit hash!
git fetch Downloads missing objects from remote into .git/objects ->
Updates remote tracking refs (refs/remotes/origin/main).
DOES NOT TOUCH WORKING DIRECTORY OR LOCAL BRANCHES!
git merge Finds common ancestor commit -> Calculates three-way merge ->
Creates merge commit and updates active branch pointer.
git pull Convenience macro: Executes "git fetch" followed immediately
by "git merge origin/<branch>".
4. The git pull vs git fetch Trap
The most common operational mistake junior engineers make is relying blindly on git pull.
When you run git pull, Git fetches remote objects and immediately attempts to merge them into your active working directory branch. If you have uncommitted changes or divergent history, this frequently triggers unexpected merge conflicts and cluttered merge commits.
The Professional Best Practice: Fetch First, Inspect, Then Rebase:
# 1. Download remote updates cleanly without modifying your local working tree:
git fetch origin
# 2. Inspect the incoming commits:
git log HEAD..origin/main --oneline
# 3. Apply your local work on top of the fresh upstream commits:
git rebase origin/main
Summary and Key Takeaways
- Git is a content-addressable graph: Files are stored as immutable hashed blobs; directories are trees; project history is a chain of commit snapshots.
- Track the four code locations: Always visualize whether code lives in the Working Directory, the Staging Index, the Local Repository, or the Remote Tracking branch.
git addwrites blobs immediately: Adding a file to staging writes compressed data into.git/objectsand updates the index file.- Commits are immutable snapshots: Git does not store diffs; each commit points to a full hierarchical tree snapshot of your repository.
- Prefer
git fetchover naivegit pull: Separate downloading remote objects from integrating them into your active working branch to prevent accidental merge conflicts.
/* Comments */
Comments are offline right now — we reconnect automatically, nothing is lost.