Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git undo command: "git reset --hard"

Im working on a Java project in intelliJ. I typed in the following command: "git reset --hard" in the terminal. How do I undo this command? Before this command I didnt commit anything. Is everything lost now?

like image 678
user7432713 Avatar asked Dec 02 '22 12:12

user7432713


2 Answers

There is 3 possibilities:

  • Your IDE has a feature to retrieve deleted file. Intellij (and all IDE by JetBrains) has this feature. Editors like VSCode, Atom, ... have extensions that could do that.

  • You already have staged your files (doing a git add) before doing the reset, so git could help you retrieve some file contents (but not file names). See In Git, how can I recover a staged file that was reverted prior to committing? and https://edwardthomson.com/blog/introducing_git_recover.html (edit: I have since then created a git command line to help recover staged files. See https://stackoverflow.com/a/58853981/717372)

  • You didn't staged files, so all your files are lost from a git point of view :( You could still try to use a file recovery tool but it could become very difficult.

PS: But don't forget one of the most important rule when using git "Commit everything and often (and especially before doing things you don't master). You will always be able to retrieve your changes/commits (at least using the reflog)"

like image 96
Philippe Avatar answered Dec 22 '22 00:12

Philippe


That is partly possible, because intelij idea stores "local history" of all edited files. AFAIK it works only separately for each file, so you have to revert all of them manualy. But still usable, right? :-)

You can access it in VCS > Local history > Show history: Show history menu

Then you may just revert to the version previous to "external change": Revert function

like image 30
zbycz Avatar answered Dec 21 '22 22:12

zbycz