How to Undo “git add” – A Step-by-Step Guide (with Examples)

Did you add a file in Git by accident and want to undo the git add command? This guide teaches you two ways you can undo git add.

As a quick overlook, there are two commands you can use to undo git add:

  1. git reset <file-name>
  2. git restore –staged <file-name>

This guide teaches you how to use both of these commands to unstage a change made by the git add command. Besides, you will learn which approach is better and why.

1. Use “git reset” to Undo “git add”

git reset to undo git add command

To unstage the change to a file, you can use git reset <file-name> to undo the git add. This is the traditional way for undoing git add commands.

For example, let’s say you’ve changed a file called example.txt in your Git project and you’ve accidentally added the change. In this case, you can undo the add by running:

git reset example.txt

This command unstages the staged changes to the file. No data is lost and you can make the necessary changes to the file before re-adding and committing.

Example

Let’s take a look at a concrete example.

I’ve made a change to a file called file.txt in my Git repo and added the change with git add file.txt.

To see the added file, let’s run the git status command:

git status to check staged files

This shows that the file.txt is staged and ready to be committed. But because I made the staging by accident, I want to get rid of it.

In this case, I can run git reset file.txt to unstage the file without losing work:

git reset command in action

After undoing git add this way, you can run git status again to see that the changes are still there but they’re no longer added. At this point, you can do some tweaks to the changes.

Notice that if you’ve added a new file to the project and want to undo adding a file, you need to use a different command.

2. git restore –staged <file-name>

git restore command to undo git add

In 2019, Git added a newly proposed solution for undoing git adds. This is the git restore –staged command:

git restore --staged <file-name>

As a matter of fact, you may have noticed that when you run git status with staged changes, it suggests using git restore –staged to unstage (undo add).

git status logs after adding changes with git add
Git CLI suggests using git restore –staged to undo add commands

Keep in mind this command is an experimental command and the behavior might change in the future!

Example

Let’s take a look at a real example that’s very similar to the previous example.

Here, I’ve made changes to a text file in my project and staged the changes using git add. To see the added file, let’s run the git status command:

git status logs after adding changes with git add

This shows that the file.txt is staged. If you want to undo this action, you can actually take a look at the git status message. It says “Use ‘git restore –staged <file>’ to unstage“. This means you can undo git add with the provided command.

In this case, I can run git restore –staged file.txt to unstage the changes made to the file without losing the changes.

git restore command in action undoing an accidental git add command

After undoing git add this way, git status shows that the changes are still there but they’re no longer staged. To re-stage, you can do git add as you would do with any other changes.

git reset vs git restore: Which One Should You Use?

This guide showed you two separate ways to undo git add. But the guide wouldn’t be complete without briefly discussing what approach you should use.

First of all, Git itself recommends using git restore –staged for unstaging files. This highly suggests you should be doing that too!

Both commands, git reset and git restore have the ability to modify the staging area and the working copy of the repo. But only the git reset command can modify the repo directly.

For example, you can do a hard reset with git reset –hard and accidentally lose local work. In this sense, it’s a safer option to use git restore to undo git add.

One reason developers still might avoid git restore is that it’s a relatively new command that was added back in 2019. More importantly, the command is still marked “Experimental“. This means its behavior might change in the future.

git official documentation for git restore experimental command
The official documentation of the git restore command.

Because of this, developers don’t want to adapt to a workflow that might change.

Summary

Today you learned how to undo the git add command.

To recap, there are two main ways to undo add commands in git:

  1. The traditional git reset <file-name>.
  2. The newly added git restore –staged <file-name>.

Thanks for reading. Happy coding!

Read Also