# The Three Trees Architecture

> Git manages your files through three distinct stages: the Working Directory, the Staging Area, and the Repository.

Subject: Version Control | Lesson 2 of 29 | 4 min read
Interactive lesson: https://codewithanimation.in/git/the-three-trees
Previous: [What is Version Control?](https://codewithanimation.in/git/what-is-version-control.md) | Next: [Initializing a Repository](https://codewithanimation.in/git/git-init.md)

## The Three Trees Architecture

To master Git, you must understand its architecture. Git doesn't just save files directly to history; it passes them through three distinct 'trees' (or areas).

1. **The Working Directory**: The physical files you see and edit on your computer's hard drive.

2. **The Staging Area (Index)**: A pre-commit holding area. It tracks what will go into your *next* snapshot.

3. **The Repository (HEAD)**: The `.git` folder where Git permanently stores all your committed snapshots.

## Why it matters

The Staging Area is Git's superpower. It allows you to selectively choose exactly which changes go into the next commit, rather than forcing you to save everything at once.

For example, if you fix a bug in `auth.js` and update a logo in `header.css`, you can stage and commit them as two separate, logical units, even if you did the work at the same time.

## How files move between them

When you edit a file, it starts modified in your **Working Directory**.

You use `git add` to move changes to the **Staging Area**. At this point, the changes are tracked and ready to be committed.

Finally, `git commit` takes everything in the Staging Area and wraps it into a permanent snapshot in the **Repository**, moving the `HEAD` pointer to this new snapshot.

## Common mistake: Skipping the staging step mentally

The #1 mistake beginners make is treating `git add .` and `git commit` as a single magic spell to 'save work'. By ignoring the Staging Area, you end up with massive, messy commits containing unrelated changes, defeating the purpose of a readable history.

## Tip: The 'Photography Studio' mental model

Think of the Working Directory as your messy photo studio. The Staging Area is the podium where you arrange the subjects exactly how you want them. The Commit is the final photograph taking the picture. You only photograph what's on the podium.

## Try it: Move a change through the three trees

Stage a working change, then commit the staged snapshot. The working file stays in place.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/the-three-trees

## Check yourself

### Question 1

Which of the 'Three Trees' represents the files you are currently editing in your code editor?

- A. The Staging Area
- B. The Repository
- C. The Working Directory
- D. The Branch pointer

Correct answer: C

The Working Directory (or Working Tree) is the actual physical files sitting on your hard drive that you modify in your text editor.

- Why not A: The Staging Area is the holding zone for the *next* commit.
- Why not B: The Repository stores permanent snapshots.
- Why not D: A branch is just a pointer to a specific commit, not a storage area.

### Question 2

What is the primary purpose of the Staging Area (also called the Index)?

- A. To back up your code to the cloud.
- B. To selectively prepare and group changes for the next commit.
- C. To permanently store a snapshot of your project.
- D. To test your code before deploying it.

Correct answer: B

The Staging Area allows you to curate precisely which modified files (or even parts of files) will be included in the next snapshot, letting you separate unrelated changes.

- Why not A: The Staging Area is purely local. Pushing backs up to the cloud.
- Why not C: This is the job of the Repository when you run `git commit`.
- Why not D: Git doesn't run or test your code.

### Question 3

Which Git command moves changes from the Working Directory to the Staging Area?

- A. git add
- B. git commit
- C. git stage
- D. git push

Correct answer: A

The `git add` command stages your modified files, placing them on the 'podium' ready for the next commit snapshot.

- Why not B: `git commit` moves changes from the Staging Area to the Repository.
- Why not C: While some aliases might do this, `git add` is the standard command.
- Why not D: `git push` uploads your local repository to a remote server.

## Remember this

- Working Directory = your messy desk.
- Staging Area = the podium where you arrange things for the photo.
- Repository = the photo album of permanent snapshots.
