Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore deleted file not staged in git

I accidentally removed the entire directory of my source code...with a nice rm -r. I know, really bad; but fortunately, I had a git repo in the containing directory. Thus, git has a huge list of unstaged changes of deleted files. For example:

"deleted:   src/caronmonitor/server.py" 

How do I get these files back? There is advice all over the web to do:

git checkout file

or

git revert <commit>

But as I understand that will restore the file to it's state at the last commit. I don't want to go back to the last commit but instead go back to right before the delete operation. I can look in the gitk and see my files as they were before the delete; thus this must be possible.

like image 977
Vance T Avatar asked Jul 23 '14 13:07

Vance T


People also ask

Can you restore deleted file in git?

If you have deleted the file and already committed the changes, you need to use the ` git checkout` command to restore the file. First, you need to find out the checksum of the commit that deleted the file, and then check out the file from the previous commit.

How do I undo a deleted file in git?

Even better, in cases like committing a file deletion and then wanting to revert it, Tower's awesome undo feature lets you get the file back by simply pressing CMD-Z!

How do I restore a file in git?

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.


Video Answer


1 Answers

I had opened the file in Eclipse IDE, so in order to restore the changes that I had lost accidentally by doing git checkout, I did as suggested below in the Eclipse IDE.

https://www.oreilly.com/library/view/eclipse-cookbook/0596007108/ch04s08.html 4.7. Restoring Elements and Files from Local History

You also can revert an entire file to its previous version by right-clicking it in a view such as the Package Explorer and selecting one of the following menu items:

Replace With→ Previous from Local History

Replaces the file with the previous version in local history

Replace With→ Local History

Replaces the file with a version you select from local history

Restore from Local History

Restores a file from local history
like image 194
Tina Avatar answered Oct 14 '22 20:10

Tina