34

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 ?

Said Kaldybaev
  • 9,380
  • 8
  • 36
  • 53
  • 1
    If you happened to `git add` these files at some point, even if you never committed them, they will still be hanging around as blobs until they're garbage collected (in about two weeks). `git fsck` will list the objects as [dangling or unreachable](https://stackoverflow.com/a/36671659/712526). You can then use `git cat-file -p deadbeef > old-file-name` to restore them. – jpaugh Aug 23 '17 at 19:36
  • ([Pod's answer](https://stackoverflow.com/a/45815093/712526) already covers this, whoops!) – jpaugh Aug 23 '17 at 19:48
  • Possible Duplicate: https://stackoverflow.com/q/25791533/712526 – jpaugh Aug 23 '17 at 19:49

11 Answers11

69

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

Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85
  • 7
    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. – Mar Dec 30 '14 at 22:47
  • 5
    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... – Haibara Ai May 30 '16 at 12:20
  • 1
    But be careful, it doesn't work with excluded directories/files in WebStorm (and other Jetbrains products) - and should not, obviously =) – Ruslan Makrenko Dec 14 '17 at 08:49
  • 1
    @RuslanMakrenko very good point :-) there is only hope that files were not excluded :-) – Jovan Perovic Dec 14 '17 at 08:53
  • Nice, I have to try this if it happens to me again. Up to now I've been using [this method](https://stackoverflow.com/a/52298924/1529709) several times, with success. – zb226 Sep 27 '18 at 23:00
  • 1
    Thank you! In PHPStorm 2019.1, right-click folder name, Local History > Show History. Then, select and revert the files to restore. – UTCWebDev May 21 '19 at 14:15
  • @JovanPerovic would you please marry me? thank you! – gdevdeiv Aug 24 '21 at 19:04
13

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

Arpan Saini
  • 4,623
  • 1
  • 42
  • 50
11

For VScode, the Below steps worked for me to restore deleted untracked file

  1. CTRL + SHIFT + P
  2. LOOK FOR: "Local History: Find Entry to Restore"
  3. SEARCH FOR DELETED FILE
  4. SELECT THE LAST SAVED FILE TO OPEN
Be Champzz
  • 391
  • 3
  • 6
7

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}

cygri
  • 9,412
  • 1
  • 25
  • 47
7

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.

Cedric Reichenbach
  • 8,970
  • 6
  • 54
  • 89
Tim
  • 2,006
  • 2
  • 18
  • 25
6

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

Pod
  • 3,938
  • 2
  • 37
  • 45
6

(Note: The steps seem to have changed a bit, since the accepted answer was posted. But the idea remains the same.)

To add to the accepted answer, I was able to recover my deleted files using Webstorm 2016.

  1. First, I lost my file doing a forced clean using git git clean -f.
  2. To recover it, I went in Webstorm and did the following steps:
  • Go to Project tab for your project, right click to get to 'Show History' under 'Local History'.

enter image description here

  • Once 'Show History' is clicked, one can see the pop up with 'External Change -> Revert' in it. Click to revert the desired files on right.

enter image description here

  • After the above 2 steps, you should see your file come back in 'Project' tab. 'Local History' pop up will show something like in this attached picture.

Reverted to External Change enter image description here

You will get an option to get it add to Git.

  • To untrack it, just do a git reset HEAD <filename> from the command line.
Anantha Raju C
  • 1,780
  • 12
  • 25
  • 35
Amit
  • 2,255
  • 1
  • 11
  • 8
3

You can't access to previous version of untracked deleted files from git, because, of coruse, they do not exist. I would recover them from some backup (maybe there are hidden backup files left by the ide/editor?), or, in alternative, I would avoid working on that filesystem too much and start searching for recovery tools.

Riccardo T.
  • 8,907
  • 5
  • 38
  • 78
  • 1
    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. – henrebotha Apr 29 '16 at 09:03
  • @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. – Arihant Jun 15 '17 at 18:27
  • @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. – henrebotha Jun 19 '17 at 06:50
  • @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. – Arihant Jun 23 '17 at 12:37
  • @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. – henrebotha Jun 24 '17 at 16:51
  • @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? – Arihant Jun 24 '17 at 17:07
0

If file retrieval tools like "recuva" didn't work, do the following:

  1. If you have build the solution at least once then there will be DLLs (In .net intermediate language files are called DLLs) stored in your Debug/Release or some other folder.
  2. Then try disassembling the DLLs using tools like DotPeek etc.

This worked for me. I was able to retrieve the content.

Sunny
  • 5,825
  • 2
  • 31
  • 41
0

If you're on Windows, go to the folder in which you kept these files > Right click > properties > "Previous revisions" and double click on a recent revision. It'll show how all of these files were at that time mentioned there. Open the files you want and if it's the one you want copy the contents and paste in your current file.

The Local History method or simply clicking ctrl+z (undo) in the editor you wrote it in, as mentioned in the other answers is the best answer. But this method came in handy when I didn't actually code in an IDE that supports local history.

0

If you are using Git, you can recover your untracked files using this command: git add <filename>

If you want to recover all of the deleted untracked files, you can do so simply with this command: git add .

A K
  • 31
  • 4