The Three Trees: Working Directory, Staging Area & Git Repository
The Three Trees: Working Directory, Staging Area & Git Repository
The single most important mental model for understanding Git is the Three States (or Three Trees) Architecture. Almost every Git command moves files between these three distinct areas.
1. The Three Sections of a Git Project
Git manages your project files through three primary zones:
Visual Architecture & Process Flow
How data and code flow step-by-step
1. The Working Directory (Working Tree)
- This is the physical folder on your computer filesystem where your project files live.
- When you open files in VS Code, modify CSS, or add images, you are directly editing the Working Directory.
- Changes here are untracked or modified, but not yet protected by Git's historical snapshot engine.
2. The Staging Area (The Index)
- The Staging Area (historically called the Index) is a file located at
.git/index. - It acts as an intermediate scratchpad where you assemble and curate the exact changes you want to include in your next commit.
- Staging gives you precise control: if you modified 5 files, you can choose to stage only 2 of them to create a focused, logical commit.
3. The Git Repository (.git directory)
- This is the permanent vault where Git stores the metadata and object database for your project.
- When you run
git commit, the contents currently in the Staging Area are permanently recorded as a snapshot commit object. - The
.gitdirectory is what gets copied when you clone a repository.
2. File Status Lifecycle
Within the Working Directory, files oscillate between two high-level states: Untracked and Tracked. Tracked files can be Unmodified, Modified, or Staged:
+------------- Edit the file ------------+
| |
v |
+-------------------+ git add +-------------------+ git commit +-------------------+
| Untracked | ----------------> | Staged | --------------> | Unmodified |
| (Brand new file) | | (Ready to commit) | | (Committed state)|
+-------------------+ +-------------------+ +-------------------+
^ |
| |
+--------- git add (re-stage) --------+
| |
v |
+-------------------+ |
| Modified | <--- Modify tracked file -+
| (Changed on disk) |
+-------------------+3. Practical Terminal Walkthrough
Let's see how files transition between states using standard terminal commands:
4. Why Does Git Have a Staging Area?
Other systems (like SVN or Mercurial) often commit directly from the working directory. Why did Git invent the Staging Area?
- Crafting Atomic Commits: You can group related modifications together. If you fixed a typo in
index.htmland wrote a new database migration indb.sql, you shouldn't bundle them into one commit. You stage and commitindex.htmlfirst, then stage and commitdb.sql. - Reviewing Diffs Before Saving: You can inspect
git diff --stagedto verify exactly what is about to enter history before finalizing the commit.
Practice Quiz
git commit -m "Add new page", what is the state of the committed file in the working directory?Installing Git and Initial Configuration (git config user.name / email)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.