How to Revert "git rm -r"
This tutorial provides the information of answering to the question of reverting git rm -r, and differences between reverting git rm -r . and git rm.
As we know, the git rm command removes a file or a folder from the working directory and adds that change to the staging area. But, sometimes, developers run it accidentally or specify an incorrect file. Here, we will demonstrate how to act in such cases.
Steps to reverting git rm -r
Below, you will find several options on how to restore deleted files or folders.
Reverting git rm with the git checkout command
First, you should execute the git reset command to revert the current staging area.
Reset the staging area
git resetAfter running <kbd class="highlighted">git reset</kbd>, you can run git checkout to restore the removed file/folder.
Restore the deleted file
git checkout <file-name>Instead of reverting the staging area and checking out the deleted file, we can use a single command to meet the same goal:
Restore directly from HEAD
git checkout HEAD <file-name>Reverting git rm with the git reset command
If there are no important uncommitted changes, then run <kbd class="highlighted">git reset</kbd> with the <kbd class="highlighted">--hard</kbd> option, which will reset everything to your latest commit:
Reset to the last commit
git reset --hard HEADReverting committed deletions
If the deletion was already committed, use git revert to create a new commit that undoes the removal:
git revert <commit-hash>Alternatively, use git reflog to find the commit before the deletion and reset to it:
git reflog
git reset --hard <commit-hash>The git rm Command
The <kbd class="highlighted">git rm</kbd> command removes tracked files from both the staging area and the working directory. It cannot delete files from the working directory alone. Note that <kbd class="highlighted">git rm</kbd> does not remove branches. It updates the staging area and working directory, but changes are only persisted after a commit. Since these changes are recorded in history, they can be undone with other Git commands.
The --hard Option
The <kbd class="highlighted">--hard</kbd> option is commonly used with <kbd class="highlighted">git reset</kbd>, but it carries risks. With this option, the branch pointer moves to the specified git commit, and both the staging area and working directory are reset to match that commit. Any uncommitted changes in the staging area or working directory will be permanently lost.
The git checkout Command
The <kbd class="highlighted">git checkout</kbd> command switches branches or restores working tree files. It works with files, commits, and branches, allowing you to navigate between different versions in a single repository. It updates the working directory files to match the specified branch or commit. Unlike <kbd class="highlighted">git clone</kbd>, which fetches code from a remote repository, <kbd class="highlighted">git checkout</kbd> operates locally to switch between existing code versions.