How to Restore Deleted Files Before Commit in Git
-
Restore Deleted Files Before Commit Using the
reset
andcheckout
Commands in Git -
Restore Deleted Files Before Commit Using the
git checkout
Command in Git - Restore a Batch of Deleted Files Before Commit in Git
We use the git checkout
and git reset
commands to restore deleted files before committing. Git provides us with powerful options to do complex tasks with these commands.
We can either unstage deleted file first and then restore it in the working tree in a separate step. Alternatively, we can combine the two operations into one step.
This tutorial shows a clever trick to batch undelete multiple files with a single command.
Restore Deleted Files Before Commit Using the reset
and checkout
Commands in Git
First, let us set up a repository and add a few files. It looks like this:
Our first couple of commits look like this in the log:
We now delete a file with the rm
command.
git rm file7.txt
The deleted file file7.txt
is no longer present in our repository.
The default behavior of rm
is to stage the delete changes automatically.
We now proceed to restore deleted file before committing.
First, we unstage the deletion with the reset
command.
git reset <commit_hash> [--] <path_to_file>
This command restores the index to the state of the commit_hash
for all files that match the path_to_file
parameter.
git reset HEAD --file7.txt
This restores the index to HEAD
for file7.txt
. HEAD
points to our last commit.
Remember, we have not committed the deletion, so our last commit does not have the deletion entry.
In essence, we use this command to unstage deleted files.
Next, we restore the deleted file in the working area with the git checkout
command.
git checkout [--] <path_to_file>
checkout
overwrites content in the working tree with the index in this form.
git checkout -- file7.txt
Restore Deleted Files Before Commit Using the git checkout
Command in Git
The git checkout
command provides us with a form where we can combine the two steps above into one.
git checkout <commit> [--] <path_to_file>
In this form, git checkout
overwrites content in both the index and working areas with commit.
git checkout HEAD -- file7.txt
HEAD
points to our last commit. We did not commit the deletion, so our last commit does not know the delete operation.
Restore a Batch of Deleted Files Before Commit in Git
What if we deleted a bunch of files and did not commit? Suppose we deleted 1000 files, and we now want to restore all of them.
Typing the above commands 1000 times isn’t a programmer’s way to do stuff. Instead, we can use wildcards in the path specifiers to match many files and undelete them with a single command.
git reset HEAD .
This is the same command as above, except we replaced file7.txt
with the .
wildcard. The .
tells git to match all files.
So, this command unstages all of our deleted files. We then restore them in the working area.
git checkout .
The same command with file7.txt
is again replaced with the .
wildcard. It restores all the unstaged deletions in one go.