Sub topic 10
The index: staging as a plumbing concept
This chapter assumes you have used git hash-object, git cat-file and git ls-tree, and know what blobs and trees are. It also assumes a Unix-style shell. The index is the missing link between loose files and the tree you hand to a commit. By the end you will be able to read it, edit it directly, and predict what git write-tree will produce.
What the index is
The index is a single binary file, .git/index. It holds a sorted list of entries, one per tracked file. Each entry records:
- the file mode, such as
100644or100755 - the object ID of the blob holding the file's content
- a stage number, normally
0 - the full path from the repository root
- cached
statdata (timestamps, size, inode) taken when the file was last checked
The index is flat. It stores docs/guide.txt as one entry with a slash in its path. It does not store a docs directory. Nested trees are built only when the index is written out as a tree.
The index is also called the staging area, but it is more than a place to park changes. It is the proposed next snapshot, and the next commit's tree comes from the index, not from your working directory. Every git add is an index update. Every git commit first writes the index as a tree.
Three snapshots are in play at any moment:
| Snapshot | Where it lives | Compared with the next one by |
|---|---|---|
| HEAD commit's tree | Object database | `git diff-index --cached HEAD` |
| Index | `.git/index` | `git diff-files` |
| Working tree | Your files | (files you edit) |
git status runs both comparisons and prints them as two columns. In AM hello.txt, the first letter compares HEAD with the index and the second compares the index with the working tree.
Watching the index appear
Run the examples in this chapter in order, in one shell. A new repository has no index file at all. Git creates it the first time something is staged.
git init index-demo
cd index-demo
test -e .git/index || echo "no index file yet"
echo 'hello' > hello.txt
git ls-files --stage
git status --shortgit ls-files --stage prints nothing, and git status --short reports ?? hello.txt. The file exists on disk but the index does not know about it.
Writing entries with git update-index
git update-index is the plumbing command behind git add. With --cacheinfo you name the mode, the object ID and the path yourself, and no file on disk is needed.
id=$(git hash-object -w hello.txt)
git update-index --add --cacheinfo 100644 "$id" hello.txt
git ls-files --stage
ls -l .git/index
git status --shortThe listing now shows the entry:
100644 ce013625030ba8dba906f756967f9e9ca394464a 0 hello.txtThe fields are mode, object ID, stage number, then a tab and the path. git status --short now reports A hello.txt.
Git refuses a --cacheinfo object ID that is not in the object database. That is why the example writes the blob with hash-object -w first. The --info-only flag skips that check, but it leaves an index that points at nothing.
Editing the file does not change the index. Git records the blob ID, not the path, so the staged content stays the same until you update it:
echo 'changed' > hello.txt
git status --short
git diff-files --name-status
git update-index hello.txt
git ls-files --stage
git diff-files --name-statusAfter the edit, status shows AM hello.txt and diff-files prints M hello.txt. Running git update-index hello.txt on a path that is already tracked hashes the file, writes a new blob and replaces the entry's object ID. The second diff-files prints nothing, because the working tree and index agree again.
Directories, modes and write-tree
When you give update-index a real file, it hashes and stores the content itself, which is what git add does. The --chmod=+x option sets the executable mode.
mkdir docs
echo 'guide' > docs/guide.txt
printf '#!/bin/sh\necho hi\n' > run.sh
git update-index --add docs/guide.txt
git update-index --add --chmod=+x run.sh
git ls-files --stage
tree=$(git write-tree)
git ls-tree -r -t "$tree"The index lists three flat paths, sorted bytewise: docs/guide.txt, hello.txt, run.sh. run.sh has mode 100755. git write-tree groups the entries by directory and writes one tree object per directory. The -r -t options make ls-tree recurse and also print the tree entries. You will see a 040000 tree line for docs followed by its child.
write-tree does not read your working directory at all. It reads only the index. If you edit a file and skip update-index, the edit is not in the tree. This is the most common surprise when building commits by hand.
Removing entries and loading trees
Use --force-remove to drop a path from the index while leaving the file on disk. git rm --cached is the porcelain equivalent.
git update-index --force-remove docs/guide.txt
git ls-files
git read-tree "$tree"
git ls-files
git checkout-index --prefix=export/ -a
find export -type fgit read-tree replaces the index with the contents of a tree. That restores docs/guide.txt to the index. It does not touch your working files. git checkout-index goes the other way and writes files out from the index. With --prefix=export/ it writes into a separate directory instead of overwriting your working tree. The prefix must end with a slash. The -a option means all entries.
The cached stat data
Hashing every file on each git status would be slow. Each entry therefore stores the file's size, timestamps, inode and similar fields as of the last update. Git rehashes a file only when its current stat data differs from the cached copy. You can see the cached values:
git ls-files --debug hello.txtThe output lists ctime, mtime, dev, ino, uid, gid, size and flags under the path. If tools touch your files without changing them, run git update-index --refresh to rebuild the cache.
The index file can also carry extensions. The best known is the tree cache, which remembers tree IDs for unchanged directories so write-tree can reuse them.
Stages and merge conflicts
Stage 0 means a normal entry. During a conflicted merge, Git instead stores up to three entries for the same path:
| Stage | Meaning |
|---|---|
| 1 | Common ancestor version |
| 2 | Your side (the current branch) |
| 3 | The other side being merged in |
git ls-files --unmerged lists them:
100644 <base-id> 1 app.txt
100644 <ours-id> 2 app.txt
100644 <theirs-id> 3 app.txtRunning git add app.txt collapses the three entries into one stage-0 entry. git write-tree refuses to run while any unmerged entries remain, so a commit cannot capture a half-resolved merge.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `cannot add to the index - missing --add option?` | The path is not yet tracked and `--add` was left off | Add `--add` |
| Commit lacks your latest edit | The edit was never staged, so `write-tree` ignored it | Run `git update-index <path>` first |
| `--cacheinfo` is rejected | The object ID does not exist in the database | Write the blob with `git hash-object -w` first |
| `write-tree` fails | Unmerged entries exist | Resolve and re-add the conflicted paths |
| `git status` shows files as modified, but the diff is empty | The `stat` cache is stale | Run `git update-index --refresh` |
Where this leads
You can now stage content by blob ID, inspect what is staged, and turn the index into a tree. The next chapter combines update-index, write-tree, commit-tree and update-ref to build a full commit by hand.
Exercise
You run echo 'v2' > hello.txt and then git write-tree in a repository where hello.txt was staged earlier with the contents v1. Which content does hello.txt have in the resulting tree, and what must you run to get the other one?
Show answer
The tree contains v1, because write-tree reads only the index and the index still points at the old blob. Run git update-index hello.txt (or git add hello.txt) first. That hashes the new content, stores a blob for v2 and updates the index entry. A second git write-tree then produces a tree containing v2.
Exercise
What does git ls-files --stage print for a file with mode 100755, and why does a file in docs/ appear with a path like docs/guide.txt rather than under a docs entry?
Show answer
It prints 100755, the blob's object ID, stage 0, then a tab and the path. The index is flat and stores full paths. Directory structure appears only when git write-tree builds nested tree objects from those paths.