Sub topic 2
The three areas: working tree, index, and HEAD
This chapter assumes you can run git init, git add, git commit, git status, and git log. It also assumes you know that a branch is a named pointer to a commit. Every undo command in the chapters ahead works on one or more of the three areas described here, so this model is the foundation for the rest of the tutorial.
The three areas
Git tracks your project in three places at once.
- Working tree: the files you see and edit on disk.
- Index (also called the staging area): a snapshot of what will go into your next commit.
git addcopies file contents from the working tree into the index. - HEAD: a pointer to the commit you currently have checked out. In normal use it points to a branch, and that branch points to the latest commit. HEAD therefore stands for the last committed snapshot.
Each arrow in the diagram is a command you will use. Forward arrows save work. Backward arrows undo it.
Watching the areas change
The easiest way to see the three areas is to change one file step by step and run git status after each step. This example creates a repository from scratch, so you can run it as shown.
git init demo
cd demo
echo "first line" > notes.txt
git add notes.txt
git commit -m "Add notes"
echo "second line" >> notes.txt
git status --short
git add notes.txt
git status --short
git commit -m "Add second line"
git status --shortThe --short output uses two columns. The left column compares the index to HEAD. The right column compares the working tree to the index.
| Step | Status output | Meaning |
|---|---|---|
| After editing `notes.txt` | ` M notes.txt` | Right column: the working tree differs from the index. Nothing is staged. |
| After `git add` | `M notes.txt` | Left column: the index differs from HEAD. The working tree matches the index. |
| After `git commit` | (no output) | All three areas hold the same content. |
The leading space in M matters. It marks an empty left column, which means no staged change.
Comparing the areas with git diff
git diff has two common forms, and each compares a different pair of areas.
echo "third line" >> notes.txt
git diff
git add notes.txt
git diff
git diff --stagedgit diffcompares the working tree to the index. Aftergit add, it prints nothing, because the two now match.git diff --stagedcompares the index to HEAD. It shows exactly what the next commit would contain.
If you ever wonder what a command will affect, run both forms first.
A file can be in several states at once
Because each area holds its own copy, one file can differ in two places. Stage a change, then edit the file again.
echo "fourth line" >> notes.txt
git add notes.txt
echo "fifth line" >> notes.txt
git status --shortThe output is MM notes.txt. The index holds the fourth line, and the working tree also holds the fifth. A commit now would include only the fourth line. The fifth stays as an uncommitted edit.
Common errors
- Assuming
git addsaves the file permanently. The index holds a snapshot of the content at the moment you rangit add. Later edits are not included until you add again. - Reading
git diffas "everything I changed". It hides staged changes. Checkgit diff --stagedas well. - Treating HEAD as a branch name. HEAD usually points to a branch, but it can point directly to a commit. That state is called a detached HEAD.
What comes next
The next chapters use this model directly. Discarding edits copies from the index into the working tree. Unstaging copies from HEAD into the index. Resetting moves HEAD itself.
Exercise
You edit app.py, run git add app.py, then edit app.py again without staging. What does git status --short show for the file, and which git diff command shows the second edit?
Show answer
It shows MM app.py. The left M means the index differs from HEAD, and the right M means the working tree differs from the index. Plain git diff shows the second edit, because it compares the working tree to the index. git diff --staged shows the first edit.
Exercise
After git commit, what does git diff --staged print, and why?
Show answer
It prints nothing. The commit made HEAD match the index, so there is no difference between them.