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 restore a deleted file from a Git repository using the git checkout command. If you do not know when a file was last deleted, you can use git rev-list to find the checksum of the commit in which that file was deleted. Then, you can check out that commit.
Press Ctrl+z for Undo & CTRL+S for saving the changes.
We can use the command git fsck to recover the files after a hard reset.
When finally executed git clean is not undo-able. When fully executed, git clean will make a hard filesystem deletion, similar to executing the command line rm utility. Make sure you really want to delete the untracked files before you run it.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With