While working with Git, it’s a common and most essential practice to commit your work regularly. Git doesn’t perform the commit automatically after you make changes in the tracked files. You need to first add your work in the staging area by using the git add command and, after that, commit it to the remote repository.
However, in some cases, you may want to undo the operation of adding files into the staging area. There can be many reasons for it, such as you don’t want the recent changes to be committed to the remote repository, or you have added too many files in the staging area, and you now feel that the files need more work before committing.
In this article, we will be discussing the question of how you can undo the addition of changed files to the staging area if you have already added them using the git add command.
Table of Contents
Two Approaches to Undo Addition of Files in Git
Following are the two commands that can serve the purpose we are looking for:
- git restore –staged
- git restore <filename>
We will be practicing each of these commands to observe their behaviors in this article, so stay tuned.
Prerequisites of Using git restore Command
The git restore command was introduced in 2.23 version of Git. Prior to this (i.e., below version 2.23), the git reset command was used to take the file back from the staging area, and after that, the git checkout — command was used to tack the files back from working directory and in its unmodified state.
Since in this article, we will be using the git restore command, so it’s important for you to update your git version to at least 2.23. Let’s do that first.
Step#01: Check Version
Check the version by using the command:
$ git --version

You can see the version is above 2.23, which is fine. But in case you have a lower version or you want to upgrade to a more new version, then check the step#02 below.
Step#02: Update Version
To update your Git version, do the following:
For Windows Users:
$ git update-git-for-windows
For Linux Users:
Follow our article on: How to install Git on Ubuntu 18.04 [Explained]
Using git restore --staged command
The easiest way to undo the git add operation is to use the git restore –staged command. The –staged option will revert the added changes from the staging area back into the working directory, which means that the file will not go to its unmodified state; it will only get back from the staging area.
$ git restore --staged file.txt
After this, your last added changes will not be a part of the staging area. You need to add them again for the next again.
Using git restore Command Without --staged Option
If you attempt to use the git restore command without –staged option, your changes will not only be reverted from the staging area but also from the working directory. This means that you will lose all the uncommitted changes, and your files will get back to their unmodified state.
$ git restore file.txt
Be careful with this command since you can’t undo uncommitted local changes.
Conclusion
Adding files and changing them is a common git operation to synchronize between your local and remote repositories. However, sometimes you need to undo the uncommitted local changes that you have added in the staging area. In this article, the process of undoing the git add transaction has been discussed with relevant pre-requisites and needed commands.