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. It builds the mental model that the rest of the tutorial relies on. Every git reset mode is a statement about which of these three areas to change, so you need to be able to see each one separately.
Why three areas
Beginners often picture Git as two places: "my files" and "the history". Git actually keeps three, and the middle one explains most of its behavior.
| Area | What it holds | Also called |
|---|---|---|
| HEAD commit | The snapshot of the last commit on the current branch | Last commit, "the tree at HEAD" |
| Index | The proposed next commit | Staging area, cache |
| Working directory | The files you see and edit on disk | Working tree, worktree |
Git moves content between these areas with specific commands. When a command seems to do something odd, it is usually because you were thinking about one area and the command touched another.
HEAD: the last commit
HEAD is a pointer to the commit you currently have checked out. Normally it does not point at a commit directly. It points at a branch name, and the branch name points at a commit. When you commit, the branch moves forward and HEAD follows it, because HEAD refers to the branch.
Each commit stores a complete snapshot of the tracked files as they were at that moment. Git stores it efficiently, but conceptually it is a full snapshot, not a list of changes. The snapshot at HEAD is the first of the three areas. You cannot edit it. A commit is immutable, and "changing" a commit really means creating a new one.
You can read it with git ls-tree and git cat-file:
git ls-tree -r HEAD
git cat-file -p HEAD:file.txtThe first command lists every file in the snapshot with its mode and blob ID. The second prints the content of file.txt as it exists in the last commit.
Index: the proposed next commit
The index is a file at .git/index. It lists every tracked path along with the content that will go into the next commit. Right after a commit or a checkout, the index matches HEAD exactly.
When you run git add file.txt, Git reads the file from disk, stores its content in the repository, and records that content in the index entry for file.txt. When you run git commit, Git builds a new snapshot from the index, not from your disk. This is why a file you edited but did not git add is not in the commit.
You can inspect the index with:
git ls-files --stage
git show :file.txtgit ls-files --stage lists the index entries. git show :file.txt prints the content that the index holds for file.txt. The leading colon means "from the index".
Working directory: your files on disk
The working directory is the ordinary folder tree where you edit files. Git does not control it. It only compares it with the index and, when you ask, overwrites parts of it. It is the only area where files can be untracked (new files Git has not been told about) or ignored (matched by .gitignore). Neither the index nor HEAD holds such files.
A worked example
Create a repository with one commit and look at all three areas.
git init areas && cd areas
echo one > file.txt
git add file.txt
git commit -m "first"
git statusOn branch main
nothing to commit, working tree cleanYour default branch name may be master depending on your configuration. All three areas now hold the same content for file.txt: one.
Now change the file on disk only:
echo two >> file.txt
git statusOn branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: file.txt
no changes added to commit (use "git add" and/or "git commit -a")Only the working directory changed. HEAD and the index still say one. Confirm each area:
cat file.txt
git show :file.txt
git show HEAD:file.txtThe first prints two lines (one, two). The other two print one.
Stage the change:
git add file.txt
git statusOn branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: file.txtNow the working directory and the index agree (one, two), and HEAD is the odd one out. Commit it:
git commit -m "second"The branch moves to a new commit whose snapshot is built from the index. All three areas agree again.
How git status reads the areas
git status is really two comparisons printed one after the other.
- HEAD against index. Differences appear under "Changes to be committed". These are staged.
- Index against working directory. Differences appear under "Changes not staged for commit". These are unstaged.
- Files in the working directory that the index does not know about appear under "Untracked files".
The same two comparisons are available through git diff:
| Command | Compares | Shows |
|---|---|---|
| `git diff` | Index to working directory | Unstaged changes |
| `git diff --staged` (or `--cached`) | HEAD to index | Staged changes |
| `git diff HEAD` | HEAD to working directory | All changes to tracked files, staged or not |
A single file can appear in both lists. If you stage a change and then edit the file again, the staged version and the on-disk version differ, so git status lists the file twice, once under each heading:
echo three >> file.txt
git add file.txt
echo four >> file.txt
git status
git diff
git diff --staged
git diff HEADHere git diff shows only the four line, git diff --staged shows only the three line, and git diff HEAD shows both. This is a good habit to build: when git status looks confusing, run all three diffs and read each one as a comparison between two named areas.
Moving content between areas
Every everyday command is a transfer between two areas.
Read the arrows as the direction content is copied:
git addcopies the working directory version into the index.git commitwrites a new commit from the index and moves the branch to it. So the index is copied "into"HEADin the sense thatHEADnow describes the same snapshot.git restore --staged <path>copies theHEADversion of a path into the index. This unstages it.git restore <path>copies the index version of a path into the working directory. This discards unstaged edits.git checkout <branch>andgit switch <branch>moveHEADto another branch, then update both the index and the working directory to match it.
Notice the asymmetry. git restore <path> takes its content from the index, not from HEAD. If you staged a version and then edited again, git restore brings back the staged version, not the committed one. To take the committed version, use git restore --source=HEAD <path>.
What the areas do with untracked and ignored files
The index only lists tracked paths. A brand-new file lives in the working directory alone until you git add it. This matters later, because commands that rewrite the working directory work from the index and HEAD. They have no record of untracked files, so they cannot restore them once overwritten, and they generally leave them alone. Keep this in mind. The safety chapter returns to it.
Common errors in the mental model
- "I committed, so my edits are saved." Only what was in the index at commit time was saved. Edits you did not stage remain in the working directory and are not in the commit. Check
git statusafter committing. - "
git addis the same as saving." The editor writes to the working directory.git addwrites a copy into the index at that moment. Later edits do not update the index automatically. - "
git commit -astages everything." It stages modifications and deletions of tracked files only. New untracked files are not included. - "
git diffshows nothing, so nothing changed." If you already staged the changes,git diffis empty. Rungit diff --staged. - "The index is a temporary detail." It is the source for the next commit, and many commands read from it. Treating it as invisible leads to surprises.
How this connects to reset
git reset works on exactly these three areas. It always moves what the current branch points at. Depending on the mode you choose, it then optionally copies the new HEAD snapshot into the index, and optionally into the working directory. Because each step reaches one area further, you can predict the effect of each mode by asking: which of the three areas will now differ from the others? The next chapters take the modes one at a time.
Exercises
Exercise
You run echo extra >> file.txt and then git add file.txt, and then run echo more >> file.txt. Which of git diff, git diff --staged, and git diff HEAD shows the line extra, and which shows more?
Show answer
git diff compares the index with the working directory, so it shows only more. git diff --staged compares HEAD with the index, so it shows only extra. git diff HEAD compares HEAD with the working directory, so it shows both extra and more.
Exercise
A file is modified in the working directory but not staged. You run git restore file.txt. Which area supplies the content that is written back, and what happens to your edits?
Show answer
The index supplies the content. Because the file was not staged, the index matches HEAD, so the file returns to its last committed content. Your unstaged edits are discarded, and since they were never committed or staged, Git has no copy to recover them from.
Exercise
You create a new file notes.txt and run git commit -m "save" without running git add notes.txt. Is notes.txt in the commit? In which areas does it exist?
Show answer
No. A commit is built from the index, and notes.txt was never added to it, so the commit does not contain it. The file exists only in the working directory, where git status lists it as untracked.