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

How to undo a 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.


Sometimes, you might accidentally stage files that shouldn't be committed yet. This guide will explore various methods to undo a Git add, using the git reset and git rm commands.

The git add command is used to add changes in the working directory to the staging area, preparing them for the next commit. When you use git add, you're telling Git to include updates to a particular file(s) in the next commit. However, if files are added to the staging area mistakenly, it's essential to know how to reverse this operation.

If you need to unstage a single file, use the git reset command:

Terminal
git reset <file>

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

Terminal
git reset example.txt

This command moves the specified file out of the staging area (but retains the changes in your working directory).

To unstage multiple files, you can list them all as arguments to the command:

Terminal
git reset file1.txt file2.txt

If you want to undo git add ., which stages all changes, you can reset all staged files back to the working directory without losing any changes:

Terminal
git reset

This will unstage all files you've added.

Another method to undo git add is using the git rm --cached command. This is particularly useful when you want to completely remove files from the staging area but keep them in your working directory, especially useful for files that were newly added to the repository and staged for the first time.

To unstage a single newly added file:

Terminal
git rm --cached <file>

For instance, to unstage a newly added example.txt:

Terminal
git rm --cached example.txt

To unstage multiple newly added files:

Terminal
git rm --cached file1.txt file2.txt

For all newly added files, you can use:

Terminal
git rm --cached -r .

This command recursively removes all files from the staging area while keeping them in your directory.

Let's say you've accidentally staged several files and a directory with the git add . command. Here’s how you can unstage them:

  1. Check what is currently staged:

    Terminal
    git status

    This command will show you all the files currently staged for commit.

  2. Unstage all changes:

    Terminal
    git reset

    This will remove all files from staging.

  3. Verify changes have been unstaged:

    Terminal
    git status

    You should see that the files are no longer staged.

For further reading 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