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?

7
  • 4
    curious: what exactly did you think that command would do?
    – Eevee
    Commented Oct 28, 2013 at 6:33
  • This question appears to be off-topic because it is about recovering files after doing a rm -r ....
    – devnull
    Commented Oct 28, 2013 at 6:33
  • I thought it would stage all of the files I had deleted. Not delete all files :(
    – Don P
    Commented Oct 28, 2013 at 6:33
  • git help rm might have helped! git-rm - Remove files from the working tree and from the index
    – devnull
    Commented Oct 28, 2013 at 6: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 Commented Oct 28, 2013 at 6:34

2 Answers 2

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.)

8
  • !@#$. They're not even in my trash. Where do they go?
    – Don P
    Commented Oct 28, 2013 at 6: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
    Commented Oct 28, 2013 at 6: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
    Commented Oct 28, 2013 at 6:50
  • those blobs ARE text files. if you want the filenames, i think you might be out of luck.
    – Eevee
    Commented Oct 28, 2013 at 6:51
  • nah i can handle the filenames :P I'm basically just doing "vi <blobnumber> 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
    Commented Oct 28, 2013 at 6:52
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

4
  • What will this revert my working directory to? Will it revert to the most local commit? In which case I lose all of my work
    – Don P
    Commented Oct 28, 2013 at 6:34
  • 2
    git reset --hard is for trashing your working directory, not recovering it
    – Eevee
    Commented Oct 28, 2013 at 6:34
  • @DonnyP try the second approach Commented Oct 28, 2013 at 6:42
  • oho, nicely done. git fsck might be easier though; amended my answer
    – Eevee
    Commented Oct 28, 2013 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.