Sub topic 10
The index: staging as a plumbing concept
This chapter assumes you can run git init, git add and git commit. It also assumes you know that git hash-object -w stores a blob and that git cat-file -p prints an object. If you skipped those, read the earlier chapters on blobs and cat-file first.
What the index is
The index is the file .git/index. It is a sorted list of paths. Each entry records a file mode, the hash of a blob, a stage number and a path. Git also keeps file metadata there, such as timestamps and sizes, so it can detect changes quickly.
The index is not a commit and not a tree. It is the proposed content of your next commit. git add copies a file into the object database as a blob and records that blob's hash in the index. git commit turns the index into a tree and wraps that tree in a commit.
Adding an entry without a file
git update-index edits the index directly. With --cacheinfo you give it a mode, a blob hash and a path. The file does not need to exist in your working directory.
git init index-demo
cd index-demo
id=$(echo 'hello' | git hash-object -w --stdin)
git update-index --add --cacheinfo 100644 "$id" hello.txt
git ls-files --stageThe output looks like this:
100644 ce013625030ba8dba906f756967f9e9ca394464a 0 hello.txtThe columns are the mode, the blob hash, the stage number and the path. --add is required because hello.txt is not yet in the index. Without it, update-index refuses to add new paths.
Now check the working directory:
ls
git status --shortls shows no hello.txt. git status --short prints AD hello.txt. The A means the file is staged as new, and the D means it is missing from the working tree. Git compares the index with both the last commit and the working tree, and that is how git status works.
Turning the index into a tree
git write-tree reads the index and writes a tree object for it. It prints the tree's hash.
tree=$(git write-tree)
git cat-file -p "$tree"
git ls-tree "$tree"Both commands print 100644 blob ce013625030ba8dba906f756967f9e9ca394464a hello.txt. cat-file -p and ls-tree agree here because they read the same object. The first compares to the second the way git ls-files --stage compares to the index: one shows the index, the other shows a stored tree.
Running git write-tree again without changing the index prints the same hash. The tree's name depends only on its content.
Common errors
- Forgetting
--add.update-index --cacheinfoon a new path fails with an error that the path cannot be added. Pass--add. - Using a hash that is not in the object database. Git refuses the entry. Run
hash-object -wfirst, because without-wthe hash is computed but nothing is stored. - Expecting
write-treeto create a commit. It only writes a tree. A commit needscommit-tree, which a later chapter covers. - Wrong mode. Use
100644for regular files,100755for executables and120000for symlinks.
Exercise
You run git update-index --add --cacheinfo 100644 "$id" notes.txt in a repo where notes.txt does not exist on disk. What does git ls-files --stage show, and what does git write-tree produce?
Show answer
git ls-files --stage lists notes.txt with mode 100644, the blob hash in $id and stage 0. git write-tree writes a tree with a notes.txt entry pointing at that blob. The working directory does not matter, because the tree is built from the index alone.