1

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?

JohnP
  • 677
  • 1
  • 9
  • 18
  • looks like this happened to someone before http://stackoverflow.com/questions/2125710/how-to-revert-a-git-rm-r – jev Sep 02 '13 at 00:34
  • @jev: considering the popular answer in there, and its quality, I am not sure that thread should be promoted. – László Papp Sep 02 '13 at 00:44
  • @LaszloPapp: I disagree; if the answer is canonically correct, then it should be promoted and linked to. Answers here aren't covering ground any newer than what was already answered in the other thread. – Makoto Sep 02 '13 at 00:47
  • @Makoto: let us agree to disagree. I believe the selected and most popular answer has low-quality, and IMHO should not be promoted until that is improved. – László Papp Sep 02 '13 at 00:48

3 Answers3

6

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.

Warning: 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.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • downvoter: mind commenting what is wrong with the answer? – László Papp Sep 02 '13 at 00:37
  • Oh, it's much better now. Although I will mention that `git reset --hard` will also overwrite any changes you have. – Makoto Sep 02 '13 at 00:41
  • I already put that into a warning. – László Papp Sep 02 '13 at 00:42
  • I don't see how it would make a difference after the user ran `git rm . -r`. @Makoto What changes would be left on it? And are you the smart guy who voted down? – konsolebox Sep 02 '13 at 00:44
  • @konsolebox: it is better not to promote a dangerous operation without a warning because users can easily get stuck to those for more than its intended scope. – László Papp Sep 02 '13 at 00:46
  • Well that could just be somebody else opinion, and I don't think that's enough to vote down an answer especially if it's enough for the intended scope. Yet the funny thing is someone's been assuming to be smarter and correct when he's not. – konsolebox Sep 02 '13 at 00:51
  • @konsolebox: everyone is entitled to his/her opinion, but it is weird he gave -1 for what he thought incorrect, but not +1 when he thought it would be correct. That is not a kinda balanced behavior. :) – László Papp Sep 02 '13 at 00:54
1

If you have no uncommitted changes, you can run these commands (in this sequence):

git reset .
git checkout .
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
0

Run git reset either soft or hard:

git reset --soft

git reset --hard

EDIT: Apparently only --hard works for it.

konsolebox
  • 72,135
  • 12
  • 99
  • 105