43

I fear I already know the answer but here goes anyway.. I accidentally clicked "Discard All Changes" in VS Code (OSX) and now a month's worth of work is gone. Poof'd. Thing is, I didn't have GIT properly setup so I hadn't done any commits yet. Is there anyway I can get my work back? Right now my working directory is as good as empty..

[edit] I should clarify, the console log says it did a "git clean -f -q" which would mean the files are gone. But I was wondering if VSCode maybe has a backup or history or cache.. or anything. I'm desperate..

user1026622
  • 541
  • 1
  • 4
  • 13
  • Possible duplicate of [Git recover uncommitted changes](http://stackoverflow.com/questions/3240436/git-recover-uncommitted-changes) – 1615903 Apr 21 '17 at 11:16
  • Also https://stackoverflow.com/questions/7147680/accidentally-reverted-to-master-lost-uncommitted-changes – 1615903 Apr 21 '17 at 11:16
  • Possible duplicate of [Accidentally reverted to master, lost uncommitted changes](http://stackoverflow.com/questions/7147680/accidentally-reverted-to-master-lost-uncommitted-changes) – PatrickSteele Apr 21 '17 at 11:57
  • 1
    Another tip (which unfortunately won't help you now, but will help going forward): [Commit early, commit often](https://blog.codinghorror.com/check-in-early-check-in-often/) – PatrickSteele Apr 21 '17 at 12:00
  • Future ref, UNLESS, you are using *PyCharm*, just open the file with the git discard changes and hit undo – ElSheikh Nov 25 '19 at 17:28

14 Answers14

54

After accidently pressed discard changes btn on the wrong file, and I ended up here) Having not found the way to restore, I have tried simple CMD+Z (CTRL+Z) in that file, and boom - everything is back, so dont give up and try that :) Very pleased

YEVY
  • 777
  • 1
  • 7
  • 15
35

That's horrible, But I got some luck !!

I was able to recover the files, where I clicked, discard all changes. No doubt, git has no option to recover those lost changes, but our system (and VScode has).

How I recovered those changes?

  1. I just opened those files again in VScode, using ctrl+p.
  2. Once opened, then I pressed ctrl+z to try undo my changes, and guess what, My system's clipboard help me recover all of my exact changes.

If you were working on a new file, that's now deleted and have no footprint now. Still you can recover the file, simply by pressing ctrl+p, coz file was already opened in VScode, with label (deleted).

In short, you can still recover your changes, provided you haven't restarted your VScode. If this solution also works for you, feel free to upvote my answer. Thanks !!

Pulkit Aggarwal
  • 595
  • 6
  • 6
18

If you can't use Ctrl+Z or Command+Z to undo or can't find your files in the trash/recycle bin, you may be able to restore using file recovery software. I was able to use the free version of Recuva from Piriform to restore the changes I accidentally discarded.

Eric Burnett
  • 191
  • 2
  • 7
13

Summary: Your work is lost, and cannot be restored.

The Clean All (discard all changes) option in VS code essentially executes:

git clean -fd
git checkout -- .

So it is git's hands whether git clean -fd can undo discarding uncommitted changes. Unfortunately the changes are not stored in git since it not committed, so after git clean -fd is executed, git will remove untracked files by force.

After executing the command git checkout -- ., git will checkout all the modified files in git as the version of last commit.

Darren Cook
  • 26,177
  • 12
  • 103
  • 206
Marina Liu
  • 35,020
  • 4
  • 56
  • 68
  • 5
    what does this actually mean ? i dont undertand anything , can some body explain ,? is there any to undo uncommitted changes ? – Shamseer Ahammed Aug 21 '18 at 05:43
  • For anyone getting here via Google: It's gone. Your only hope is a disk backup, previous commit or your recycle bin (Windows) / Trash (OSX). – user1026622 Feb 18 '19 at 11:17
  • 6
    I strongly disagree with this accepted answer. As stated in the answers of roslyn and EFFG, the files might be recovered on VS Code using ctrl+Z or in the trash bin. – Thomas Vuillemin Apr 26 '19 at 11:54
  • 1
    @ThomasVuillemin even if the files are possibly in the trash bin, the question was related to git. Fact is that the action in itself is an irreversible action, unfortunately. – user1026622 Apr 27 '19 at 20:44
  • @user1026622 well, that's the thing : reading the question it seems to be not just about git, but also VS Code, and a Google research to both leads you here, while there are others questions just about git. And even if the git action is irreversible, in the specific case of doing it in VS Code, there are way to to revert it. I think the answer to this specific question should reflect that. – Thomas Vuillemin Apr 28 '19 at 09:45
  • I got stuck with 5k changes and vscode refused to discard changes. Hitting those commands did the trick! – Yasser Jarouf Apr 29 '19 at 20:06
  • @user1026622, you saved the day man, thank you!! I had accidentally removed a file, I clicked discard and it reverted back to the last commit way before the latest uncommitted changes, I found the actual file in my recycle bin !! – TheeBen Aug 04 '20 at 06:23
6

CMD+Z / CTRL+Z in that file will RECOVER even if you changed your branch or closed your file )

WQTM
  • 511
  • 6
  • 5
4

I have done the same thing on my Windows PC right now in GitHub Desktop client. I thought that it would revert what was UNCOMMITED but it decided to revert it all. Ehh.

Luckily for me, I have checked the Bin and all the discarded files were there (together with the unwanted ones as well which I wanted to remove in the first place that I got rid of more carefully afterwards, restoring these files first).

Not sure whether MacOS also moves them to Trash. Double check, you might be lucky ;)

roslyn
  • 356
  • 3
  • 6
1

I discard a new file change by mistake, and I can not find it by cmd+p in the selection list, and I recover the new file back in the end, but it works just for some cases that there is another file that keeps the file path reference, I use the other file path reference by click import xxx from ./xxx/losted_File.js, and the file come back(delete), and I save the deleted file, and the File recovered.

Brance Lee
  • 121
  • 1
  • 4
0

I just had this issue. I pressed the "discard changes" arrow next the wrong file in VS Code (before adding and committing). My previous file was not in the recycling bin and after researching, CTRL+Z/CMD+Z was the only way to go. If you've already closed the file, then you may unfortunately be out of luck.

Nic_H
  • 11
  • 1
0

On the source control all the project is there: retrieving the content of the project to vs code explorer is the issue. The answer is change the view of the project such that you can view all the content without the folders. Then command + A to select all and press open and once all are open they will be available on vs code explorer

0

Please see the below screen shot from where you can discard all changes or you could do one by one as well.

enter image description here

Amit kumar
  • 415
  • 6
  • 13
0

In my case, I have discarded a config.yaml file in Elementory OS from my laravel project run through DDEV, so in order to get it back to my current project, I went to the trash and used ls -al to show hidden folders and then entered the hidden folder to copy what I need to my project folder with the cp command.

0

Please install plugin Local History https://marketplace.visualstudio.com/items?itemName=xyz.local-history

There is no way to recover files/changes if you did not commit them in git, unless you cache changes locally

new2cpp
  • 2,601
  • 4
  • 24
  • 34
0

VSCode recently added a new feature for recovering local history: https://github.com/microsoft/vscode/issues/26339

Demo: https://www.youtube.com/shorts/6SosuQDWaYU

This allows you to recover any Git changes you discard.

-1

In my case, I mistake discard changes from the branch. This code was on commit bat not pushed. I go to {projectPath}.git/logs/refs/heads/ and find a file with a branch. In this file, I find the list off headers. Then in the console, I use git reset {commitHash} to undo commit.

Yuri
  • 9
  • 3