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

Understanding the Git staging area

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

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


The staging area in Git acts as a prep zone for changes before they are committed to the main repository. This guide provides an in-depth look at how to interact with the staging area, including adding, modifying, and removing changes.

The staging area, also known as the index, is a layer between the working directory and the repository. It allows developers to fine-tune what goes into their next commit rather than committing all of their changes at once. This can be particularly useful for grouping related changes into logical commits, even if they were made at different times.

To begin using the staging area, you must first add changes to it. This is done with the git add command.

  1. Open your terminal.
  2. Navigate to your Git repository.
  3. Add a single file to the staging area:
    Terminal
    git add path/to/your/file.txt
  4. Alternatively, to add all changes in the directory (including new files and modifications), use:
    Terminal
    git add .

These commands move the current state of the files into the staging area, ready to be included in the next commit.

After staging some changes, you can view what is currently staged before committing by using:

Terminal
git status

This command will list items in the staging area in green, indicating that they are ready to be committed. To see the actual content changes in detail, use:

Terminal
git diff --cached

This shows the differences between the last commit and what is currently staged.

If you've made additional changes to a file after it has been staged, those changes won't be included until you add the file to the staging area again:

Terminal
git add path/to/your/file.txt

This command updates the file’s status in the staging area with the latest changes.

If you decide that some changes should not be committed yet, you can remove them from the staging area without altering the working directory using the git restore command with the --staged option:

  1. To unstage a specific file: Use the git restore --staged command followed by the file path. This command will unstage the changes made to the specified file but keep the changes in your working directory:

    Terminal
    git restore --staged path/to/your/file.txt
  2. To remove all changes from the staging area: To unstage all changes, use the git restore --staged command with the dot (.) to specify all files in the current directory:

    Terminal
    git restore --staged .

These commands revert the staging area to match the last commit, but leave your working directory unchanged.

  • Staging patches: Sometimes, you might want to stage only parts of the changes made to a file. Git allows you to add changes interactively:

    Terminal
    git add -p

    This command lets you review changes chunk by chunk, deciding whether to stage them.

  • Ignoring files: To prevent certain files from being added to the staging area accidentally, use a .gitignore file. Add patterns for files you want to ignore, and Git will no longer prompt you to stage those files.

  • Recovering staged changes: If you accidentally remove something from the staging area, you can typically recover it by re-staging the changes from your working directory as long as you haven’t deleted them.

For further reading on the Git staging area, see the official Git documentation.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2