Data report"State of code review 2024" is now liveRead the full report

Git index

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


The Git index is a temporary staging area (also referred to as the "staging area" or "cache") used to prepare changes before committing them to the project history. When you perform actions such as git add, Git does not immediately commit the changes to your repository. Instead, it adds the changes to the index.

This guide will explain what the index is, how it works, and its relationship with other Git components like the HEAD.

The index is a binary file (.git/index) that stores a sorted list of file names, along with file metadata and pointers to the object database—the files that contain the contents of these files at the time they were added to the index. The object database is located in .git/objects.

  1. Adding changes to the index: When you modify a file in your working directory and execute git add <file>, Git updates the index to include the new version of the file. This does not affect the current branch until you commit the changes.

  2. Viewing the index: You can see which files are in the index (staged for the next commit) by running git status. Files listed under "Changes to be committed" are in the index.

  3. Removing changes from the index: If you decide not to commit a file that you have added to the index, you can use git reset HEAD <file> to remove it from the index but keep the changes in your working directory.

The HEAD in Git is a reference to the last commit on the current branch. It is a pointer to the current branch reference, which in turn is a pointer to the last commit made on that branch. When you make a commit, Git takes the data in the index and creates a new commit out of these data, moving the HEAD to point at the new commit.

The process involves several steps:

  • The index holds a prepared snapshot: Immediately before you commit, the index holds the exact contents of what will go into the commit.
  • Committing changes: When you run git commit, Git converts the state of the index into a tree object, and then creates a commit object that points to this tree and the commit that HEAD pointed to before.
  • Updating HEAD: After the commit, HEAD (and the branch it points to) is updated to refer to the new commit.

Here's a brief look at common commands interacting with the index:

  • Adding a file:

    Terminal
    git add README.md

    This command updates the index with the contents of README.md.

  • Viewing staged changes:

    Terminal
    git status

    This shows the current state of the index versus the working directory.

  • Committing the index:

    Terminal
    git commit -m "Update README.md"

    This command commits the contents of the index to the repository.

For further reading on the Git index, see the official documentation.

On this page
Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Learn more

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2