Book Your Free Call

How To Undo The Git Add Command?

Tim Mouskhelichvili
Tim Mouskhelichvili
2 minutes to read

Git is the most popular version control software because it offers a simple process for tracking code changes. To push some code changes, you first need to stage them using the git add command. This brings the question of how to undo the git add command.

To undo the git add command, use the git reset command like so:

bashgit reset HEAD file.txt

This article will explore different ways of undoing the git add command.

Let's get to it 😎.

git undo add

The definition

The git add command adds the changes from the working area to the staging area.

Only when the changes are in the staging area will the developer be able to commit them.

Example

Let's say you've modified a file called index.js.

To add it to the staging area, use the git add command.

Then, using the git status command, check the status of your changes:

bash> git status
On branch main
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:  index.js

How to undo the git add command?

Two ways exist to undo this command:

  • Using the git reset command.
  • Using the git restore --staged command.

How to undo using the git reset command?

You can undo the git add command using the git reset command.

This command will remove the changes from the staging area AND keep the changes in the working area.

To remove one file from the staging area, type:

bashgit reset HEAD index.js

To remove all the files from the staging area, type:

bashgit reset HEAD

Note: HEAD points to the latest commit of the active branch.

How to undo using the git restore command?

You can also undo the git add command using the git restore command.

The git restore command was added in version 2.23 of Git and is used to restore working tree files.

To remove one file from the staging area, use:

bashgit restore --staged index.js

To remove all the files from the staging area, use:

bashgit restore --staged .

How to undo a directory added by git add?

To remove a directory from the staging area, use the git rm cached command.

bashgit rm --cached -r path/to/directory

Final thoughts

As you can see, undoing the git add command is easy.

Just use the git reset or git restore --staged command.

Both commands will keep the changes in the working area.

To remove the changes from the working area, use the git checkout command.

git undo add

Here are some other Git tutorials for you to enjoy:

Comments (0)