
Module 1 Lesson 3: Git architecture: working directory, staging area, repository
Understand the 'Three Trees' of Git. Learn how your files move from your folder to the 'Index' and finally into the 'Git Repository' history.
Module 1 Lesson 3: Git architecture
Most new Git users are confused by one specific thing: "Why do I have to 'Add' my files before I 'Commit' them?"
The answer lies in Git's internal architecture, often referred to as the Three Trees. Understanding these three states is the "Aha!" moment that makes everything else in Git click.
1. The Three States
A Git project consists of three separate "areas" where your code lives at different points in time.
Area 1: The Working Directory
This is simply the folder on your computer where your project files are stored. When you edit a file in VS Code or Notepad, you are working in the Working Directory.
- State: "Modified" (if you've changed files but haven't told Git yet).
Area 2: The Staging Area (or "The Index")
This is a middle-ground area. It’s like a "loading dock" for your next commit. You choose which specific changes from your Working Directory you want to include in the next version.
- State: "Staged" (you've marked a modified file to go into your next snapshot).
Area 3: The Git Directory (The Repository)
This is where Git stores the metadata and object database for your project. This is the part that is copied when you clone a repository. Once a file is here, it is safely stored in history.
- State: "Committed" (the data is safely stored in your local database).
graph LR
WD["Working Directory (Edit)"] -- "git add" --> SA["Staging Area (Select)"]
SA -- "git commit" --> GD["Git Directory (Store)"]
GD -- "git checkout" --> WD
2. Why do we need the Staging Area?
In older systems, you committed everything you changed all at once. Git gives you a "buffer."
Example Scenario:
- You are working on a new "About Us" page.
- Halfway through, you notice a typo on the "Home" page and fix it.
- You want to commit the typo fix now, but your "About Us" page isn't finished yet.
With Git, you can git add index.html (the typo fix) and commit just that, while leaving the half-finished about.html in your Working Directory. The Staging Area allows for clean, atomic commits.
3. The Lifecycle of a File
Every file in your working directory can be in one of two states: Tracked or Untracked.
- Untracked: Files that were in your last snapshot and any files you haven't added yet.
- Tracked: Files that Git knows about. They can be Unmodified, Modified, or Staged.
stateDiagram-v2
[*] --> Untracked
Untracked --> Staged: git add
Unmodified --> Modified: edit file
Modified --> Staged: git add
Staged --> Unmodified: git commit
Unmodified --> Untracked: remove file
Lesson Exercise
Goal: Visualize the Three Trees.
- Draw three boxes on a piece of paper: "My Desk," "The Mailbox," and "The Archive."
- Imagine you are writing 10 different letters.
- You finish 3 letters and put them in the Mailbox. Are they in the Archive yet?
- You change a word in a letter still on your Desk. Does the Mailbox version change?
- How does this analogy map to the Working Directory, Staging Area, and Repository?
Observation: You'll realize that the Staging Area is your control center for what the world (or your future self) will eventually see in the Archive.
Summary
In this lesson, we established:
- Working Directory: Where you edit files.
- Staging Area: Where you prepare changes (loading dock).
- Git Directory: Where history is stored forever.
- The distinction between Tracked and Untracked files determines if Git is watching your changes.
Next Lesson: It's time to get your hands dirty. We'll learn how to install and configure Git so you can start using these three trees.