Git

How to Unstage a Deleted File in Git

On Git, developers can create new files and modify the existing files, as well as they can remove the files from the repository or just from the file system. It depends on the developer’s requirements. Moreover, they can unstage or recover deleted files. The “git reset — <file-name>” command can be used to revert the file’s status in the index.

This study will describe the process of unstage deleted files in Git.

How to Untrack a Deleted File in Git?

Suppose the user created files in their repository. But later they realized these files are unused and want to delete them from the repository. Additionally, they don’t want to update the Git repository with these changes and are required to unstage the deleted files. For this purpose, the “git rm <file-name>” command can be used.

Let’s move forward and implement the previously discussed scenario!

Step 1: Go to Git Repository

To redirect to the Git repository, the “cd” command:

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

Step 2: Create and Modify File

Now, make a new file and update it immediately by running the “echo” command:

$ echo "1 text file" >> file1.txt

Step 3: Stage Modifications

Next, add changes to the staging index through the “git add .” command:

$ git add .

Step 4: Update Local Repository

Then, push the staged changes to the Git repository by utilizing the “git commit” command with the “-m” option:

$ git commit -m "file1.txt added"

Step 5: Make and Update File

Similarly, run the “echo” command to create a new file and add some text to it simultaneously:

$ echo "2nd text file" >> file2.txt

Step 6: Stage changes

To add the working area changes to the repository, use the following command:

$ git add .

Step 7: Commit Added Changes

Now, commit the changes to the Git repository with the help of the following command:

$ git commit -m "file2.txt added"

Step 8: Remove Particular File

Next, execute the “git rm” command to remove the particular file:

$ git rm file1.txt

Here, the provided file is deleted successfully:

Step 9: View Git Repository Status

After that, check the status of the current working directory through the following command:

$ git status .

According to the below-given output, the file is deleted:

Step 10: Unstage Deleted File

Finally, to unstage the removed file, use the “git reset” command along with the file name:

$ git reset -- file1.txt

It can be observed that the specified file is unstaged:

Step 11: Check Repository Status

Lastly, to ensure whether the deleted file is staged successfully or not, use the below-given command:

$ git status .

As you can see, the “file1.txt” deleted file is unstaged successfully:

That’s it! We have compiled the method of unstage deleted files in Git.

Conclusion

To unstage deleted files in Git, first, we will create files and then provide the process of unstage deleted files. To do so, move to the desired repository and generate a new file. Then, track it and push it to the Git repository. Similarly, generate and track another file. Next, update the repository by committing. After that, run the “git rm” command with the particular “<file-name>”. To unstage the deleted files, run the “git reset — <file-name>” command. This study illustrated the process of unstage deleted files 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.