Read Anthropic’s case study about Graphite Reviewer

How to remove files after running git add

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

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


In Git, the git add command is commonly used to stage files for the next commit. However, sometimes you may stage files accidentally or to decide later that you don't want certain files to be committed. This guide provides a detailed overview of how to unstage these files, effectively reversing the git add operation.

The git add command updates the index using the current content found in the working tree. It typically prepares the introduction of new changes to the repository via a commit. However, if changes are added to the index mistakenly, Git provides ways to revert this operation before a commit is made.

  1. Unstage a single file

    If you've staged a file by mistake and need to remove it from staging (unstage it), you can use the git reset command:

    Terminal
    $ git reset HEAD <file-path>

    Replace <file-path> with the actual path to the file you want to unstage. This command will not alter the file itself; it only removes the file from the staging area, leaving any changes you made intact in your working directory.

    For example, to unstage a file named example.txt run:

    Terminal
    $ git reset HEAD example.txt
  2. Unstage multiple files

    To unstage multiple specific files, you can use the git reset HEAD command followed by each file path, separated by a space:

    Terminal
    $ git reset HEAD file1.txt file2.txt file3.txt

    This command removes file1.txt, file2.txt, and file3.txt from the staging area.

  3. Unstage all changes

    If you want to unstage all files that you have added, use the following command:

    Terminal
    $ git reset

    This command will remove all files from the staging area without altering the files themselves in your working directory.

Starting with Git version 2.23, you can use the git restore command to achieve similar results in a more intuitive way. Here’s how:

  • Unstage a single file:

    Terminal
    $ git restore --staged <file-path>

    This command will unstage the file specified.

  • Unstage multiple files:

    Terminal
    $ git restore --staged file1.txt file2.txt
  • Unstage all staged files:

    Terminal
    $ git restore --staged .

    This command unstages all files currently in the staging area.

  • Verify before staging: Always use git status to check which files are staged and unstaged. This helps prevent accidental staging of unwanted files.
  • Stage files incrementally: Rather than using git add . to stage all changes, add files incrementally, and consider using git add -p to stage by hunk to avoid staging unnecessary or sensitive files.
  • Use .gitignore: Utilize a .gitignore file to automatically exclude certain files and directories from being staged, such as log files, build directories, or temporary files.

For further reading on how the git add command works, see the official Git documentation.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

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