Git

How to Unstage File in Git

Git is a popular DevOps tool frequently used for managing and testing the source code of various projects from small to large scale. The source code files are classified into two categories: Staging files and Unstaging files. More specifically, Unstaging files are untracked files that have not been added to the repository’s tracking index, whereas staging files are tracked files.

This write-up will illustrate how to unstage the Git files.

How to Unstage Files in Git?

Sometimes developers want to revert the committed changes and try to restore the previous version of the project. For this purpose, it is required to unstaged stages files and committed files.

To unstaged the staged or committed files, we have provided the methods listed below:

How to Unstage Stage Files?

To unstage a staged file, utilize the Git “restore” command. To do so, we have listed down a procedure that is effective enough.

Step 1: Open Git Bash Terminal

From the Window Start menu, launch the Git Bash terminal:

Step 2: Change Working Repository

Next, change the Git local repository through the “cd” command:

$ cd "C:\Git"

Step 3: Create New File

Create a new file with the help of the “touch” command and specify the file name:

$ touch File1.txt

Step 4: Add File to Staging Area

Next, add the newly created file in the Staging area by utilizing the “git add” command:

$ git add File1.txt

Let’s move to the next step.

Step 5: Check File Status

Check the file status to verify whether the file is added in the staged area or not:

$ git status

You can see the file is now in the staged area:

Step 6: Unstage the Staged File

Now, move the staged file into the unstage area by utilizing the “git restore” command:

$ git restore --staged File1.txt

Verify whether the file is unstaged or not through the “git status” command:

$ git status

The below output shows that we have successfully unstaged file in Git:

How to Unstage Committed Files?

To unstage the committed file in the Git repository, follow the below provided instructions.

Step 1: Add Files in Staging Area

First, add untracked files in the staging area using the provided command. Here, the “.” sign will stage all files and directories in the unstaged area:

$ git add .

Step 2: Check File Status

Check the repository status using provided command:

$ git status

Here you can see we have added File1.txt, File2.txt, and a directory demo1 in the staging environment:

Step 3: Commit the Staged Files

Commit the staged files using the “git commit” command along with the “-m” flag to add a message:

$ git commit -m "All files are committed"

Step 4: Check Log

Print out the Git log to see committed changes:

$ git log

The below output shows that staged files and directory are committed:

Step 5: Unstage Committed File

To unstage the committed file, utilize the “git rm –cached <Filename>” command as it removes the specified file from the Git cache:

$ git rm --cached File1.txt

To verify whether the committed file is unstaged or not, execute the “git status” command:

$ git status

Here, you can see, the file is deleted and added to the unstaged area:

We have learned how to unstage files in Git.

Conclusion

Users can unstaged the staging files and committed files. To unstage the staged files, first, open the Git repository and utilize the “git restore –staged <Filename>” command. To unstage the committed files, utilize the “git rm –cached <Filename>” command on the Git bash terminal. In this write-up, we have illustrated how to unstage files in Git.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.