Sub topic 2
The three areas Git tracks: HEAD, index, working directory
This chapter assumes you can run git init, git add, git commit, git status, and git log, and that you know what a branch is. Every git reset mode is defined by which of the three areas it changes, so you need this model first.
The three areas
Git keeps your work in three places at once.
| Area | What it holds | How you change it |
|---|---|---|
| HEAD commit | The last commit on your current branch, a saved snapshot | `git commit` |
| Index (staging area) | The proposed next commit | `git add`, `git restore --staged` |
| Working directory | The files you see and edit | Your editor, `git checkout`, `git restore` |
HEAD is a pointer to your current branch, and the branch points to its latest commit. When this chapter says "HEAD", it means the snapshot in that commit.
The index is a list of files with their contents, ready to be committed. A commit records the index, not the working directory. This is why a change you edited but did not git add does not appear in the commit.
How changes move between areas
A normal workflow moves changes from right to left in the table, one step at a time.
git reset works in the opposite direction. It copies content from the HEAD commit back toward the index and the working directory. The next chapter covers exactly how far it copies.
Seeing the three areas in practice
Create a repository with one committed file, then change it in stages.
git init areas && cd areas
echo one > file.txt
git add file.txt
git commit -m "first"
echo two >> file.txt
git add file.txt
echo three >> file.txtNow file.txt has a different version in each area:
- HEAD commit:
one - Index:
oneandtwo - Working directory:
one,two, andthree
Check this with two diff commands.
git diff
git diff --stagedgit diff compares the working directory to the index. It shows only the three line. git diff --staged compares the index to the HEAD commit. It shows only the two line.
git status reports the same thing in two sections. "Changes to be committed" lists differences between the index and HEAD. "Changes not staged for commit" lists differences between the working directory and the index.
git statusThe file appears in both sections, because it differs at both boundaries.
Reading a state before you reset
Before you run any reset, ask which area holds the change you care about. The answer tells you which mode is safe.
- Changes in the HEAD commit are saved in history. A reset can move the branch away from them, but they are still recoverable.
- Changes in the index but not in a commit are less protected. Git has stored the file contents as objects, but no branch points to them.
- Changes only in the working directory are the most exposed. Git has no copy of them anywhere.
This ordering explains why --hard is the dangerous mode. It is the only mode that overwrites the working directory.
Exercise
You run echo a > f.txt, git add f.txt, git commit -m "add f", then echo b >> f.txt and git add f.txt, then echo c >> f.txt. What does git diff show, and what does git diff --staged show?
Show answer
git diff compares the working directory to the index, so it shows only the added line c. git diff --staged compares the index to the HEAD commit, so it shows only the added line b.
Exercise
Which of the three areas does a commit record: the index or the working directory? What follows for a file you edited but did not run git add on?
Show answer
A commit records the index. An edited file that was not added is not part of the commit, and its changes stay only in the working directory.