Git does not automatically involve changes in a commit: they have to be explicitly added to the next commit, with the git add command. But, in any instance, you may need to undo a “git add” operation that you just made.
Also Refer: Git Commands
So this tutorial is all about undoing the git add command. Go through the following modules and completely learn how you can easily undo a Git add command with ease. With the help of these methods, your git files can be back in your working directory.
- Git undo Add Operation
- Determine your current Git version
- How to undo git add before commit?
- Undo Git Add using restore
- Undo Git Add using reset
- Erasing your local changes using git checkout
As a picture is worth a thousand words, here is a recap of all the operations that you can perform to undo a Git add operation.
If you want to know more details, you should go through the detailed examples below.
Also Check: How To Undo Last Git Commit
Like with any other software tool, Git changes and adds new commands on major releases.
As a result, if you are using an old version of Git, you might not be able to run some of the commands.
In order to know your current Git version, you can execute the “git” command followed by the “–version” option.
$ git --version
The “git restore” command was introduced in Git 2.23, so if you are using an older version, make sure to update your current Git installation.
$ git update $ git update-git-for-windows
While working in Git, the files to be committed are found in the staging area. Now, assume that you have added an unnecessary file to the staging area. To remove only a single file which is unuseful from the staging area should run the command below:
git reset <file-name>
If you want to remove all the files from the staging area, then you should run the following command:
As you can perceive, git reset is utilized for undoing git add. Running only git reset without defining the file name is particularly helpful when you are inadequate to list all the actual files one by one.
The simplest way to undo your git add command is to use the “git restore” command with the “–staged” option and specify the file you want to unadd.
$ git restore --staged <file>
As an example, let’s say that you are on a branch named “feature” and that you have recently added a new file to your staging area.
$ git status
In order to bring back the file to the working directory, we can execute the “git restore” command with the “–staged” option.
$ git restore --staged another-file
Congratulations, you have successfully undone your “git add” command using the restore one!
Now you can modify your file, add it again and commit it if you want.
The “–staged” option means that you essentially want to unstage the file from your Git index, but that you want to keep the modifications done to the file.
So what happens if you don’t specify the “–staged” option?
As specified in the “git restore” documentation, by default, the working tree will be restored, meaning that you discard changes from your working area.
Note that the file needs to be tracked by Git in order to be restored: if it is part of the untracked files, you won’t be able to “restore” it.
As an example, let’s say that you have modified a file but the changes are yet to be committed to the staging area.
Note: Be extra careful with the “git restore” command when files are in your working directory, you will lose all your changes and there is no going back.
$ git restore another-file $ git status On branch feature nothing to commit, working tree clean
Awesome, your file went back to the state it was before doing any modifications to it.
Undoing a recent Git “add” operation can also be achieved by using the “git reset” command followed by the file that you want to “unstage“.
$ git reset <file> $ git reset -- <file> (is an equivalent syntax) $ git reset HEAD <file> (achieves the same result)
As an example, let’s try to undo a Git “add” operation on a file named “file” that we just added.
In this case, to undo this accidental Git “add” operation, we are going to use the “git reset” command, specify the HEAD (the most recent commit) and the name of the file.
$ git reset HEAD file Unstaged changes after reset: M file
As you can see, our files were correctly unstaged which is the result that we wanted to achieve.
A simple “git status” command can be used in order to verify our current state.
Congratulations, you have successfully undone an “add” operation in your repository!
Now that your file is back to your working directory, you can choose to erase all the modifications that you have performed.
To undo all local changes and go back to your file original version, you have to use the “git checkout” command followed by double hyphens and the name of the file.
$ git checkout -- <file>
As an example, let’s use the file that we unstaged in the previous section.
If we want to remove the modifications done to this file, we execute the command described just above.
$ git checkout -- file
As you can see, all modifications were “erased” and the file is back to its original state.
In this tutorial, you learned how you can easily “undo” a Git “add” operation. Depending on your Git version, you can use two commands: the “restore” one, or the “reset” one.
Additional options have to be used depending on the location of your file: “–staged” if it is in the staging area or no options if it is in your working directory.
If you are interested in Git, we have a complete section dedicated to it on the website, so make sure to check it out!