This question already has an answer here:<ul><li> <a href="/questions/2125710/how-to-revert-a-git-rm-r" dir="ltr" rel="nofollow">How to revert a “git rm -r .”?</a> <span class="question-originals-answer-count"> 11 answers </span> </li> </ul>
Testing with the
rm command in Git I ended up removing all my files from my drive.
The command that caused this to happen is the following:
git rm . -r
Now my file system contains only the directories while all the files that I have previously added to Git are missing.
How can I restore all the files from the Git repository?Answer1:
git checkout .
In general, if it happens again to you, but with a slightly different scenario like only deleted a few files, you can still use the same pattern:
git checkout -- <file_path>
In that case, the output of
git status will also help you with providing possible solutions.
<strong>Warning</strong>: For this particular case, you can also run
git reset --hard, but the solution above looks a bit cleaner. Do not get used to this command so easily because you might wipe your modifications away in the future accidentally.
If you have no uncommitted changes, you can run these commands (in this sequence):
git reset . git checkout .Answer3:
git reset either soft or hard:
git reset --soft
git reset --hard
EDIT: Apparently only
--hard works for it.