Is it still possible to restore deleted untracked files in git?

41,812

Solution 1

Some better IDEs keep track of your files as a local history. If you removed files externally (say, git reset) you should be able to click in your IDE on parent directory and choose "Compare with local history".

I used this feature successfully in PHPStorm IDE when my untracked files got wiped out by some utility...

Solution 2

In Intellij how to recover the untracked revert changes

Step 1: Select the Folder , Right click on it

Setp 2: Go to Local History, and then go to Show history

enter image description here

Step 3: Select the untracked files that your revert accidentally. then select those file in right side panel and revert the changes, you will get the files in reverted format.

enter image description here

Solution 3

git reset --hard is a very dangerous command, so be careful when you use it next time :)

If you do not have any commit for those files, it seems you have no chance restore them.

Otherwise, reflog command can help you.

Solution 4

git fsck may be able to help you.

If you have not done garbage collection yet it might be able to help you. Once you have the blob id you can say:

git show {blobId}

Solution 5

git reset --hard won't remove untracked files. git clean will, however.

However if you git add the files, don't git commit them and then run git reset --hard those files will be lost.

But it's ok, as they can be recovered, as shown in this answer. (Well, until the next git gc).

Share:
41,812
Said Kaldybaev
Author by

Said Kaldybaev

Trying to make world better

Updated on August 23, 2020

Comments

  • Said Kaldybaev
    Said Kaldybaev almost 4 years

    let's say yesterday I did some changes on my master branch, and I forgot to add, commit them. and in the morning i did

    git reset --hard
    

    is it possible to restore deleted files in this situation ?

  • Martin
    Martin over 9 years
    For Eclipse, navigate to the folder/package that the file was in (Project Explorer or Package Explorer view), right-click, and select Restore from Local History... from the context menu.
  • henrebotha
    henrebotha about 8 years
    Good call on the editor! I had discarded untracked changes from a file by accident (curse the Github app) but realised I still had the file open in Atom and could simply hit "undo" to get my changes back.
  • Haibara Ai
    Haibara Ai almost 8 years
    omg... You saved my life. I was using git reset --hard in a zero commit project, which means git fsck --lost-found not work. Luckily I read your answer and use revert history in WebStorm and successfully got the wiped codes...
  • Arihant
    Arihant almost 7 years
    @henrebotha, hi! this just happened with me, but unfortunately I didn't have my atom opened at the point of git hard reset, do you know of any possible way I could be saved. Very foolishly I had tons of things in my untracked files.
  • henrebotha
    henrebotha almost 7 years
    @gdad-s-river You should still be able to open Atom up after the fact and hit undo, as long as you edited the file in Atom at some point.
  • Arihant
    Arihant almost 7 years
    @henrebotha, the problem is that I had closed the files in the atom before I closed the atom itself so there were no open buffers of files to undo.
  • henrebotha
    henrebotha almost 7 years
    @gdad-s-river Unless Atom changed how it works (or you configured it in a strange way), you should be able to open the files up and hit undo.
  • Arihant
    Arihant almost 7 years
    @henrebotha, I don't know about if Atom changed, but I hadn't configured it in a strange way. Do you know a strange atom config way that would enable one to do that?
  • Ruslan Makrenko
    Ruslan Makrenko over 6 years
    But be careful, it doesn't work with excluded directories/files in WebStorm (and other Jetbrains products) - and should not, obviously =)
  • Jovan Perovic
    Jovan Perovic over 6 years
    @RuslanMakrenko very good point :-) there is only hope that files were not excluded :-)
  • zb226
    zb226 over 5 years
    Nice, I have to try this if it happens to me again. Up to now I've been using this method several times, with success.
  • UTCWebDev
    UTCWebDev about 5 years
    Thank you! In PHPStorm 2019.1, right-click folder name, Local History > Show History. Then, select and revert the files to restore.
  • gdevdeiv
    gdevdeiv over 2 years
    @JovanPerovic would you please marry me? thank you!
  • Adi
    Adi over 2 years
    ohhh man !! you saved my day :D