Git

What are the Two Ways to Unstage a File in Git?

When Git users work with multiple collaborative project files, sometimes they unintentionally perform incorrect operations, such as tracking an incorrect file to the staging area or including sensitive information before committing to the Git local repository. In such cases, they may want to unstage or delete the file. For the stated purpose, “$ git reset” or “$ git rm –cached” commands can be utilized.

This guide will demonstrate:

Let’s try them out one by one!

How to Untrack a File in Git Using “git reset” command?

To unstage a file using the “git reset” command, firstly, move to the desired repository and create a new file. Then, track into the staging area and commit changes to update the repository.

Now, execute the above-stated procedure to get results!

Step 1: Move to Git Repository
First, run the “cd” command and move to the Git local repository:

$ cd "C:\Users\nazma\Git\Test_1"

Step 2: Create File
To create a new file, run the “touch” command with the specified file name:

$ touch file2.txt

Step 3: Track File
Now, track the file to the Git local repository through the “git add” command:

$ git add file2.txt

Step 4: Check Status
Execute the provided command to view the repository’s current status:

$ git status .

The below output indicates that the newly created “file2.txt” is tracked and placed in the staging area:

Step 5: Unstaged File
Now, unstaged the file by utilizing the “git reset” command along with “” and desired file name:

$ git reset -- file2.txt

According to the below output, the file is unstaged successfully:

Step 6: Check Status
Lastly, execute the “git status .” command to verify the previously performed operation:

$ git status .

Let’s have a look at another method to unstage a file in Git.

How to Untrack a File in Git Using “git rm –cached” command?

Another way to unstage a file is using the “$ git rm –cached <file-name>” command. To do so, move to the desired repository and create a new file. Then, track into the staging area and commit changes to update the repository. Lastly, execute the “$ git rm –cached <file-name>” command.

Try out the below-given steps to perform it practically!

Step 1: Track File
First, run the “git add” command to track the file:

$ git add file2.txt

Step 2: Check Status
To check the repository status, execute the below-stated command:

$ git status .

Here, the file is placed in the staging area and ready to commit:

Step 3: Unstage File
Now, unstage a file using the “git rm” command along with the “–cached” option:

$ git rm --cached file2.txt

Step 4: Verify Unstaged File
Lastly, to verify if the file is unstaged, execute the “git status .” command:

$ git status .

It can be observed that the file is unstaged successfully:

We have mentioned the two ways to unstaged a file in Git.

Conclusion

To unstage a file using the “git reset” command, firstly, move to the desired repository and create a new file. Then, track into the staging area and commit changes to update the repository. Execute the “$ git reset — <file-name>” command. Another method to perform a similar operation is to execute the “$ git rm –cached <file-name>” command. This guide provided the two methods to unstage a file in Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.