Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git accidentally deleted all of my changes

Tags:

git

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?

like image 656
Don P Avatar asked Oct 28 '13 06:10

Don P


People also ask

How do I undo a git deletion?

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.

Can you recover discarded changes in git?

Press Ctrl+z for Undo & CTRL+S for saving the changes.

How do I revert changes after git reset?

We can use the command git fsck to recover the files after a hard reset.

Can you undo a git clean?

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.


2 Answers

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

like image 61
Eevee Avatar answered Oct 14 '22 14:10

Eevee


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

like image 5
Martin Seeler Avatar answered Oct 14 '22 13:10

Martin Seeler