What is the Index

The index (also called the staging area or cache) is the binary file .git/index. It records the state of files that will go into the next commit — a manifest of blob hashes, file paths, and metadata.

How it works

When you run git add main.rs, Git hashes the file's contents, creates a blob in the object database, and updates the index entry for main.rs with the new blob hash, file size, and modification time.

When you run git commit, Git reads the index and creates a tree that matches it. The commit points to that tree. The index is not cleared — it continues to reflect the committed state.

git status works by comparing three things:

  1. HEAD tree vs index — shows staged changes (what git commit will include).
  2. Index vs working tree — shows unstaged changes (what git add would stage).

The index caches file metadata (mtime, size, inode) to make git status fast. If a file's metadata matches the index, Git skips reading its contents — most files in a large repository have not changed.

Why it matters

The index is Git's mechanism for building commits incrementally. You can stage specific files (or even specific hunks within a file with git add -p) to construct exactly the commit you want, rather than committing everything at once.

See How Git Internals Work for how the index, working tree, and object database interact.