How to Unstage a Deleted File in Git

There are cases when you have deleted a file and add the deletion to the staging area. Here, we will discuss how to unstage the deletion and restore the deleted file.

Watch a course Git & GitHub - The Practical Guide

Steps to unstaging a deleted file

Let’s discuss how to restore that one file without discarding other changes. Assume you need to undo the result of git rm <file-path>.

Unstaging the deleted file

Now, you need to unstage the deleted file with the git reset command as follows:

git reset -- <file-path>

Undoing the deletion

Here, you need to run the git checkout command to restore the deleted file:

git checkout -- <file-path>
Use double dashes to instruct Git to check out a file, instead of checking out to a branch.

The git checkout Command

The git checkout command switches branches and restores working tree files. The git checkout -- <file> command is used to discard changes in the working directory. However, take into account that this command is dangerous. Any local changes you made to that file are gone. Do not use this command until you are sure you do not need these unsaved local changes.

The git reset Command

The git reset command is a complex tool used to undo changes. It has three basic forms of invocation. These forms correspond to the command line options: --soft, --mixed, --hard. Each of the options corresponds to Git's three trees: the commit tree ( HEAD), the staging area (index), and the working directory. The working directory represents the files on the file system of the computer available to the code editor for applying changes. The staging index tracks the changes committed in the working directory. The commit history ( HEAD) changes and points to the currently checked out branch, and the last commit on it through that branch.