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?
-
Nothing (except uncommitted changes) is lost. Just checkout the commit from the reflog, and create a branch from it. – evolutionxbox Dec 16 '17 at 17:35
-
@evolutionxbox I didnt commit anything. – user7432713 Dec 16 '17 at 17:36
-
This wasn’t an answer but a comment. – evolutionxbox Dec 16 '17 at 17:36
-
Have a look here: https://github.com/k88hudson/git-flight-rules/blob/master/README.md#i-accidentally-did-a-hard-reset-and-i-want-my-changes-back – evolutionxbox Dec 16 '17 at 17:42
-
@evolutionxbox I tried this comment "git reset --hard d4541t8" but it doenst work. – user7432713 Dec 16 '17 at 18:00
-
What about it doesn’t work? – evolutionxbox Dec 16 '17 at 19:26
-
1I would recommend not type `git reset --hard` in future unless you intend to discard uncommitted changes – M.M Dec 16 '17 at 22:46
-
Possible duplicate of [How can I undo git reset --hard HEAD~1?](https://stackoverflow.com/questions/5473/how-can-i-undo-git-reset-hard-head1) – kowsky Dec 18 '17 at 06:32
5 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
)"

- 28,207
- 6
- 54
- 78
-
5All 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
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:
Then you may just revert to the version previous to "external change":

- 626
- 9
- 12
-
1Currently 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
If you haven't made a git gc
you can use the git reflog
.
https://www.atlassian.com/git/tutorials/rewriting-history/git-reflog

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

- 813
- 5
- 16
I use PHPStorm, but it's highly likely it's the same in any jetBrains IDE, including intelliJ:
- Go to the Project tool window and right-click the project node or just a folder, where the file used to exist.
- 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.
- Have fun! :)
Source: https://blog.jetbrains.com/idea/2008/01/using-local-history-to-restore-deleted-files/

- 5,536
- 7
- 36
- 59