C.W.K.
Stream
Lesson 03 of 05 · published

The Three Areas: Worktree, Index, Repository

~22 min · staging, index, workflow

Level 0Untracked Rookie
0 XP0/47 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Three areas, three responsibilities

Almost every confusion in Git traces back to mixing up the three areas. Internalizing them once will save you years of guesswork. The three are the working tree, the index (staging area), and the repository.

The working tree is the regular files on disk you edit. It has nothing to do with Git directly — it is just the current checkout. Editors, build tools, and your eyes interact with the working tree.

The index, also called the staging area, is the most surprising layer. It is a binary file at .git/index that holds a proposed next snapshot. When you say git add file.js, you are not "marking the file for commit" in some abstract list — you are copying the current content of file.js into the index as a blob and updating the index entry. If you change file.js again afterward, the index still has the version you added. This is why git diff shows working tree vs index, and git diff --staged shows index vs last commit.

The repository is everything under .git/: objects, refs, the index file itself, hooks, configs, packed history. git commit takes the current index, freezes it as a tree object, wraps it with metadata into a commit object, and points the current branch at that commit. Three areas, three commands: git add moves working tree → index, git commit moves index → repository, git restore moves either direction back.

Code

Watch the three areas diverge·bash
echo 'v1' > note.txt
git add note.txt              # index now has v1, working tree v1
echo 'v2' > note.txt          # working tree v2, index still v1

git diff                       # working tree vs index → shows v1 → v2
git diff --staged              # index vs last commit → shows nothing → v1
git status -s                  # both flags shown: index (left) and worktree (right)
Restore moves data back across the boundaries·bash
# Pull index version back over working tree (discard local edits)
git restore note.txt

# Pull last commit back into index (unstage)
git restore --staged note.txt

# Pull last commit all the way back to working tree (rewind both)
git restore --source=HEAD --staged --worktree note.txt

External links

Exercise

Create a tiny repo with one file. Stage version A with git add. Edit the file to version B without re-adding. Run git diff, git diff --staged, git status -s. Now run git commit -m 'check'. Inspect the resulting commit with git show. Confirm in writing which version landed in the commit and why.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.