5

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?

user7432713
  • 197
  • 3
  • 17

5 Answers5

10

There is 3 possibilities:

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

Philippe
  • 28,207
  • 6
  • 54
  • 78
  • 5
    All IDEs produced by JetBrains keep a history of the changes of the project files. @user7432713, you can recover some of your uncommitted changes from it. Look for the "Local History" command in the application menu (it stays under VCS in PhpStorm and RubyMine, I don't have a copy of IntelliJ IDEA at hand to check). – axiac Dec 16 '17 at 18:21
9

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? :-) Now (2022) you can right click a whole directory and select this option as well.

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

zbycz
  • 626
  • 9
  • 12
  • 1
    Currently it works for the whole project: https://www.jetbrains.com/help/clion/local-history.html#restore-files-from-local-history – Andrei Mar 23 '22 at 07:14
  • Thanks a ton for this. Saved my day! FYI: Intellij let's you find local history at directory level too. Just right click on the directory to get the option. – Rohit Lakhotia Feb 04 '23 at 15:06
1

If you haven't made a git gc you can use the git reflog.

https://www.atlassian.com/git/tutorials/rewriting-history/git-reflog

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • If you have open changes that changes should be open after the git reset or you should get an error message, that you have open changes but i'm not really sure. – René Höhle Dec 16 '17 at 17:24
  • @Stony Ty for your answer. I dont really know anything about the terminal. I typed in the command "git reflog". I see a bunch of rows like "HEAD@{13}". What should I do know? – user7432713 Dec 16 '17 at 17:30
1

If files has not been committed/staged then git reset --hard cannot be undone.
The only you can do to recover your work in this case is to use Local History to recover your files.

user882813
  • 813
  • 5
  • 16
1

I use PHPStorm, but it's highly likely it's the same in any jetBrains IDE, including intelliJ:

  1. Go to the Project tool window and right-click the project node or just a folder, where the file used to exist.
  2. On the context menu, choose Local History, and click Show History on the submenu: The local history view for a project or folder shows you everything that you have done during the last few days.
  3. Have fun! :)

Source: https://blog.jetbrains.com/idea/2008/01/using-local-history-to-restore-deleted-files/

benomatis
  • 5,536
  • 7
  • 36
  • 59