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