6

In git I did git rm -r .

This was stupid, because apparently it deleted all of my files in my working directory. Many of these files had changes that had not been commited. I was being stupid and didn't commit anything for the past week.

How can I undo this? I want to get back all the changes in my working directory NOT the last checkin on git.

When I run git status, it shows about 200 files that were deleted. Many of these had changes in my working directory that I would like to keep. How can I do this?

Don P
  • 60,113
  • 114
  • 300
  • 432
  • 4
    curious: what exactly did you _think_ that command would do? – Eevee Oct 28 '13 at 06:33
  • This question appears to be off-topic because it is about recovering files after doing a `rm -r ...`. – devnull Oct 28 '13 at 06:33
  • I thought it would stage all of the files I had deleted. Not delete all files :( – Don P Oct 28 '13 at 06:33
  • `git help rm` might have helped! `git-rm - Remove files from the working tree and from the index` – devnull Oct 28 '13 at 06:34
  • Sorry to hear that but `rm -r` is as dangerous as it can get. One of those things which come with a label `Handle with care` – Hanky Panky Oct 28 '13 at 06:34
  • i believe you wanted `--cached`, then, but i sympathize; this is a pretty bad UI decision on git's part. in the future consider using `git add -u`, which just makes the index match your working directory for files git already tracks, including deleted files. – Eevee Oct 28 '13 at 06:36
  • Git is one big usability defect. – garryp Apr 24 '15 at 19:33

2 Answers2

14

You can't. Git deleted the files. They are gone. You can maybe undelete them with some file recovery tool, but that's well beyond the scope of git.

Not committing frequently sorta defeats the purpose of using a VCS.


edit: I take it back! Git is amazing and might have made a blob for your files (I assume for the purposes of checking freshness with git-status).

This thread suggests using git fsck --lost-found to find git objects that aren't yet part of the repository. It'll dump a bunch of files into .git/lost-found/other; with any luck some will be your work. (This is a somewhat easier way to accomplish the same thing as git prune -n, mentioned below and in this answer.)

Community
  • 1
  • 1
Eevee
  • 47,412
  • 11
  • 95
  • 127
  • !@#$. They're not even in my trash. Where do they go? – Don P Oct 28 '13 at 06:33
  • 3
    nowhere. they are just loose bits on your hard drive. if you intend to try disk recovery software, you need to stop writing to the drive **now** before something overwrites them, but honestly you're probably better off trying to recreate your work while it's still fresh in your head. – Eevee Oct 28 '13 at 06:34
  • So now the files in .git/lost-found/other all appear to be blobs, how can I turn them back into my original text files? – Don P Oct 28 '13 at 06:50
  • those blobs ARE text files. if you want the _filenames_, i think you might be out of luck. – Eevee Oct 28 '13 at 06:51
  • nah i can handle the filenames :P I'm basically just doing "vi but getting back a mix of my files' original contents, but then strange characters as well: ` /* Base styles * ------------------------- */^F; T; ;^K;^F[^@;^Li^K;^M@^Ko:^YSass::Tree::RuleNode^L: @rule[^FI"^K.alert^F; T:` – Don P Oct 28 '13 at 06:52
  • oh, yes, there may very well be some unrelated junk in there, especially if you haven't done a `git gc` in a while. – Eevee Oct 28 '13 at 06:53
  • Git/You saved my 3 days of work. Thanks a ton. Good that I did "git add ." before doing the "git rm -rf" command. I think it wouldn't have been possible without that add command. – nizam.sp Apr 05 '15 at 19:15
  • how to recover with the blobs ? – Albert James Teddy Dec 16 '16 at 11:41
5

The good thing is, in git it is not lost.

Try git reset HEAD to revert your changes in your index and then git checkout . to check out your files from HEAD


Another way if you have not commited them is to execute git prune -n and search the blob numbers. You can then restore them by using git cat-file -p <blob-hash #> , see git cat-file

Martin Seeler
  • 6,874
  • 3
  • 33
  • 45