2

I did a git hard reset without committing my files. After firing reset I checked all my untracked files are gone. I am expecting it will affect only the changes we made during the commit and it will simply reset those changes. But I was wrong.

Is there any way to get recover all my untracked files ? Any Help would be really really appreciated.

Thanks

  • You need to follow whatever procedure is proper for your OS in recovering deleted files. The first step is to stop using that drive. Then try to find some file recovery software to see if it can find the file allocations on the disk and relocate them. Just Google "recovering deleted files on ......." – Matt Runion Aug 27 '18 at 15:04
  • 2
    git reset --hard should not remove untracked files – schildi Aug 27 '18 at 15:06
  • @schildi I thought the same thing at first but I assume it' just a confusion in the terms with *uncommitted changes* upon reading the question body. – Romain Valeri Aug 27 '18 at 15:31
  • 1
    Possible duplicate of [Used git reset --hard, now wants to undo it](https://stackoverflow.com/questions/37093056/used-git-reset-hard-now-wants-to-undo-it) – phd Aug 27 '18 at 15:36
  • https://stackoverflow.com/questions/47848185/git-undo-command-git-reset-hard – phd Aug 27 '18 at 15:36
  • https://stackoverflow.com/search?q=%5Bgit%5D+undo+reset+hard – phd Aug 27 '18 at 15:36

1 Answers1

2

If you previously did a git add with your untracked files, then git reset --hard will delete them. Git has no process of restoring these because they are untracked. You will need to find another tool to do so, such as your operating system or IDE. IntelliJ has a "Local History" tool that you might be able to leverage for restoring these files.

For future reference, prefer git stash to git reset --hard. git stash creates a temporary "stash" commit with your changes. These can be easily retrieved with git stash apply or git stash pop. When you nuke your working directory with git reset --hard, it is much more difficult to undo, sometimes even impossible.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 2
    Note that if you have done `git add`, Git saved the files as objects in the database, and you can use `git fsck --lost-found` to retrieve their content for about two weeks from the `add` time. It's a bit painful (or a lot painful) as the *names* are gone. – torek Aug 27 '18 at 15:43
  • 1
    But it could help if you successfully sort them by file creation date. – Philippe Aug 27 '18 at 21:03