Signup/Sign In

How to Undo Git Add Command

The Git Add command is used to add files to the staging area or the index. Files are added to the staging area so that they are included in the next commit. But sometimes we may end up adding unwanted files to the staging area and don't want these files to be a part of the next commit. Git provides us with a few different commands to undo a Git Add command. Let's learn how to do this.

Undo Git Add

Undo Git Add

Two different commands can be used to undo the effects of Git Add. We have the Git Reset command, and we also have the newer Git Restore command. Let's learn more about these commands.

Undo Git Add using Git Reset

The Git Reset is a powerful command that can be used to rewrite the history of our project. It works in three modes - soft, mixed, and hard. We will be using the --mixed option to undo the Git Add command. It is also the default mode in which Git Reset works so we do not need to explicitly mention this mode.

$ git reset <file-name>

The above command will remove the mentioned file from the staging area. We can also empty the staging area and undo the effects of $ git add -all command by simply running the Git Reset command.

$ git reset

Consider the following examples to understand the working of the Git Reset command when used to undo the Git Add command.

Suppose, we have three files added to the Staging Area.

Checking the status of our repository before unstaging files.

Let's first remove just one of these files.

Using Git Reset to undo the effects of Git Add on a single file

and then check the status.

Checking the status after removing a file from the staging area.

Now, let's unstage all the files.

Using plain Git Reset command to unstage all changes

Emptying the staging area using the Git Reset command

Undo Git Add using Git Restore

The Git Restore command is fairly new and was added in version 2.23 of Git. To undo the effects of a Git Add command on a certain file i.e. to remove a file from the staging area, use the --staged option with the command.

$ git restore --staged <file-name>

If we omit the --staged option then even the changes made to the file will be discarded and it will be restored to its last committed version. Use this with caution as we may end up losing our unsaved changes.

The following images illustrate the working of the Git Reset command.

We initially have three files in our staging area.

Checking the status of our repository before undoing a Git Add.

Let's restore one of them and check the status of the repository.

Running the Git Restore command with --staged to undo a Git Add for a file

Now check the status again.

Using Git Status after restoring a file

Summary

Staging is a process of organizing and segregating the files that we want to add to the next commit. The Git Add command is used to stage the changes made to the files. But sometimes we may end up staging unnecessary changes and later want to undo the effects of Git Add on those changes. This is known as Unstaging. Git provides us with the Git Reset and the Git Restore command to undo the effects of Git Add. We can simply pass the name of the file that we want to unstage to these commands and that file will be removed from the staging area. Remember to use the --staged option with Git Restore as we may end up losing important changes if we omit it.