简体   繁体   中英

Git accidentally deleted all of my changes

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM