Git

How to Revert Stage Files in Git?

Git is a well-liked DevOps tool widely used for managing source code of all types of projects. It maintains code files in two types of environments: Staging area and Unstaging area. More specifically, Staging files are tracked files, while unstaging files are untracked files that have not been added to the tracking index of the repository.

Usually, developers test different files and features on the project, and sometimes, the user does not want to add them to the project repository and wants to revert them to the unstaging area.

This post will provide methods to revert stage files to the unstage area:

So, let’s start!

Method 1: Revert Stage Files Using Reset Command in Git

To revert the stage files in Git, users can use the Git reset command. For this purpose, check out the below-given procedure to revert stage files.

Step 1: Open Git Bash
First, launch the Git Bash terminal from the Start menu:

Step 2: Go to Git Repository
Next, open the Git working repository using the “cd” command:

$ cd "C:\Git"

Step 3: Make New File
Make a new file in the Git repository as we will create “File1.txt”:

$ touch File1.txt

Check the file status to verify whether the file is created or not:

$ git status

It can be observed that the file is created successfully, but it has not yet been tracked and is shown under untracked files:

Step 4: Add File to Staging Area
Next, add the file to a staging area using the provided command:

$ git add File1.txt

Again, check the file status:

$ git status

Here, you can see the file is now added in the staging area and is ready to commit:

Step 5: Revert Stage File to Unstage Area
Revert the stage file to the unstage area using the git reset command:

$ git reset File1.txt

Step 6: Check File Status
Verify whether the file is reverted from the staging area:

$ git status

Here you can see we have successfully reverted stage “File1.txt” to the unstage environment:

Method 2: Revert Stage Files Using Restore Command in Git

To revert the stage file to an unstage area with the help of the git restore command, check out the procedure.

Step 1: Add File to Stage Environment
Utilize the “git add” command and specify the file which needs to be added in the stage area:

$ git add File1.txt

Now, move to the next step to verify whether the file is added to the stage area or not.

Step 2: Check File Status
Check out the file status to verify if the file is added to the stage or not:

$ git status

It can be observed that the specified file is ready to commit:

Step 3: Revert File to Unstage Area
To revert the staged file to the unstage area, run the “git restore” command along with the “–staged” option as follows:

$ git restore --staged File1.txt

Again, execute the “git status” command to verify whether the file is reverted to the unstage environment or not:

$ git status

The below output shows that the file is reverted to the unstaged area:

We have taught you how to revert the stage files in Git.

Conclusion

To revert the stage files in Git, first, open the Git working repository and check the file status to verify if any file exists in the staging area or not. Then, utilize the “$ git reset <Filename>” command to revert the file to the unstaging area. Alternatively, you can use the “$ git restore –staged <Filename>” command. This post has taught you how to revert stage 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.