How to Unstage Files on Git
The Staging Area or the Index is the place where we add files so that Git can start tracking them. It is a way of organizing our files and preparing them for a commit. But there may be cases when we want to remove files from the staging area. This is called Unstaging. Let's learn how to do that in Git.
- Staging Area is an intermediate step between making changes to our project and storing those changes permanently as a commit. We reach the staging area by using the Git Add command.
- Git will only start tracking changes to files when they are added to the staging area.
- The staging area is needed because it allows us to better plan our commits. We could make multiple changes in our working directory and then add only a few of them to the staging area for a commit.
There may be cases where we add some files to the staging area but later decide to not include them in the next commit. To make sure that these staged files are not included in the commit we need to unstage them. Git provides us with a few different commands to do this.
Git Reset Command
Git Reset is used to reset changes. These can reset our repository, staging area, and even the working directory. Let's learn how to unstage files using Git Reset.
To unstage a specific file we can simply use the Git Reset command.
$ git reset <file-name>
If we do not pass the file name to Git Reset, then all the changes will be unstaged. The staging area will be empty.
$ git reset
Let's see an example of how it works. Consider we have 3 files added in the staging area.
Let's first remove just one file from the staging area.
We can empty the staging area by not passing any file names to the command. This will remove the remaining two files
Git Restore Command
Git Restore is a new command and was added in version 2.23 of Git. This command can also be used to remove files from the staging area. We need to use the --staged option with it to remove files from the staging area. After unstaging a file if we run the Git Restore command without the --staged option, even the changes made to a files are restored to the previous committed version of the file.
$ git restore --staged <file-name>
Let's take a look at an example. Consider we have a file with a single line of text that was committed previously.
Now let's add a new line to this file.
Let's add this file to the staging area.
Now if we run the Git Restore command with the --staged option the file is removed from the staging area.
After unstaging if we run the Git Restore command without the --staged option then the file would be restored to the previously committed version and changes will be lost.
Git Rm Command
The Git Rm command can also be used to undo the staging of a file. This command is mostly used for removing newly created files that you have added to the staging area. When used for unstaging modified files, the Git Rm command will remove the file from the staged area but the file will also become untracked rather than becoming unstaged. It will also stage the removal of the file in the staging area when used for modified files.
$ git rm --cached <file-name>
Let's understand this command with the help of the following example. Consider we have two files in the staging area - one of them is a newly created file and the other one was modified.
Now when we use the Git Rm command on the newly created file, it is simply removed from the staging area and becomes untracked.
But when we use the same command on the modified file, then this file also becomes untracked and the removal of this file is added to the staging area.
The Staging Area is an important part of Git Workflow. It makes our commits atomic which in turn makes it easier to understand our project. However, there will be cases where we have to remove some changes from the Staging Area. We learned that this can be done by using the Git Reset command, the Git Restore Command, and the Git Rm command.