How to Restore Deleted Files Before Commit in Git
This tutorial demonstrates restoring deleted files before commit in Git.
- Restore Deleted Files Before Commit Using the reset and checkout 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.
1. 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.
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.
1 | 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.
1 | 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.
1 | git checkout [--] <path_to_file> |
checkout
overwrites content in the working tree with the index in this form.
1 | git checkout -- file7.txt |
2. 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.
1 | git checkout <commit> [--] <path_to_file> |
In this form, git checkout
overwrites content in both the index and working areas with commit.
1 | 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.
3. 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.
1 | 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.
1 | git checkout . |
The same command with file7.txt
is again replaced with the .
wildcard. It restores all the unstaged deletions in one go.
- Title: How to Restore Deleted Files Before Commit in Git
- Author: Abel
- Created at : 2024-07-14 06:25:59
- Updated at : 2024-07-26 12:37:21
- Link: https://fix-guide.techidaily.com/how-to-restore-deleted-files-before-commit-in-git/
- License: This work is licensed under CC BY-NC-SA 4.0.