Sub topic 5
The working tree, the staging area, and the repository
This chapter assumes you can use a terminal, have Git installed, and have already run git init in a project directory. It explains where your changes live at each step, which makes the later commands easier to predict.
Three places a change can live
Git keeps your work in three areas.
- The working tree is the set of files you see and edit in your project directory.
- The staging area (also called the index) is a list of the changes you have chosen for the next commit.
- The repository is the history of commits stored in the
.gitdirectory.
A change moves from left to right. You edit a file in the working tree. git add copies its current content into the staging area. git commit turns everything in the staging area into a new commit in the repository.
Why there is a staging area
The staging area lets you decide what goes into a commit. Suppose you fix a bug and also tidy some unrelated formatting. You can stage only the bug fix and commit it alone, then commit the formatting separately. Each commit then describes one idea, which makes history easier to read and to undo.
Watching a change move through the areas
git diff shows how the areas differ from each other. This sequence makes it visible:
echo "More details." >> README.md
git diff
git add README.md
git diff
git diff --stagedHere is what each command shows:
- After the edit, the first
git diffprints the new line. It compares the working tree with the staging area, and the two differ. - After
git add, the secondgit diffprints nothing. The staging area now matches the working tree. git diff --stagedprints the new line. It compares the staging area with the last commit, and the two differ.
The two forms of git diff compare different pairs:
| Command | Compares | Shows |
|---|---|---|
| `git diff` | Working tree and staging area | Changes you have not staged yet |
| `git diff --staged` | Staging area and last commit | Changes the next commit will contain |
Staging captures a moment
git add copies the file as it is at that moment. If you edit the file again after staging it, the new edit is not staged. The file then appears in both lists in git status: once under changes to be committed, and once under changes not staged for commit.
echo "First line" > notes.txt
git add notes.txt
echo "Second line" >> notes.txt
git statusA commit made now would contain only "First line". Run git add notes.txt again to include the second line.
Reading the areas from git status
git status groups files by area. Files under "Changes to be committed" are in the staging area. Files under "Changes not staged for commit" are modified in the working tree only. Files under "Untracked files" exist in the working tree and Git has never recorded them. The next chapter looks at this output in detail.
Exercise
You edit app.py, run git add app.py, and then edit app.py again. Which git diff command shows the second edit, and what will git commit record?
Show answer
Plain git diff shows the second edit, because it compares the working tree with the staging area. git commit records only the version staged by git add, so the second edit is left out until you run git add app.py again.