78

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
  • 901
  • 1
  • 6
  • 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

18 Answers18

146

I just know only one solution to resolve this problem. You must remember every file name which you have changed.

Step 1: Enter Ctrl + Shift + P on VSC

Step 2: Choose the option "Local History: Find Entry to Restore"

Step 3: Find the file which you want to restore

Step 4: Choose a time you save that file

Step 5: Copy file content and restore it

enter image description here

enter image description here

enter image description here

yyater97
  • 1,697
  • 2
  • 9
  • 5
71

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
  • 1,062
  • 1
  • 12
  • 16
  • nice to hear that, but it didn't worked on Ubuntu 18, If it works for someone on Ubuntu18, please give solution(which might be useful for me next time. :P ) – GD- Ganesh Deshmukh Jun 22 '20 at 10:32
  • While that would work if it immediately happened, this obvs won't work for files that were reverted a while ago and the VSCode had been closed since. In which case, the local history answer is the way to go. – Studocwho Jul 07 '23 at 23:07
38

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.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Pulkit Aggarwal
  • 623
  • 7
  • 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
  • 27,837
  • 13
  • 117
  • 217
Marina Liu
  • 36,876
  • 5
  • 61
  • 74
  • 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
  • 7
    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 !! – TheBen Aug 04 '20 at 06:23
7

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

wriozumi
  • 569
  • 6
  • 6
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
  • 391
  • 3
  • 8
2

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
  • 457
  • 6
  • 14
2

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.

ExplosiveFridge
  • 190
  • 1
  • 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
  • 131
  • 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

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
  • 3,311
  • 5
  • 28
  • 39
0

Ctrl+Z or restore from trash.

I have lost work to this multiple times, and to remedy this, I commit all the changes, then reset the branch to the previous commit.

git add -A
git commit -m "Discard"
git reset --hard HEAD^

To do this faster, i have created a git alias. Run:

git config --global --edit

And add this line to the end of the file:

[alias]
  discard = "!sh -c 'if [[ ! -z $(git status -s) ]]; then git add -A && git commit -m "Discard" -q && git reset --hard HEAD^ -q && echo "discarded"; else echo "no changes"; fi'"

Then you can run git discard to discard the changes, but keep a reference in the reflog. See this question on how to restore the commits from the reflog. This is a form of stash, but I usually only use the stash when moving branches or to keep for longer.

Zikoat
  • 373
  • 3
  • 9
0

Problem: I was working on creating data structures and algorithms codebase in Python. There were around 12 files. I wanted to store them on my personal GitHub account So I did git init which added all the files to the untraced state. I thought I was doing something wrong So I clicked on the 'Discard All Changes' option available on VSCode and boom! all the files are gone, and the directory was completely empty.

Conclusion: 'Discard All Changes' will permanently remove the changes/files. So there's no way to recover them using Git. The only options available are VS Code way (the first 3 solutions) and System way(the final solution).

Note: I've considered almost all the answers given above and prepared this list.

Solution:

  1. I've installed the 'Local History' extension as mentioned by one of the answers but the file history was completely empty for me. Also, I couldn't remember all those 12 file names. - Failedenter image description here
  2. VS Code has a TIMELINE feature recently added but the file history list was also empty for me. - Failed
  3. For my luck there were 4 files opened in VSCode while I was doing 'Discard All Changes" actions So those 4 files had the content available but the label was changed to 'deleted'. I tried opening the recent files using Ctrl+P / CMD+P but only these 4 files were showing So I just recovered them. Use Ctrl+Z or just save them if the file was already opened or try opening them using Ctrl+P. - Partial Success (4 recovered out of 12)
  4. Before jumping on to the final solution which completely restored my files. I want to talk about how the delete feature works. Basically, files are stored in different memory locations. When you delete a file, the system doesn't remove the data it just labels that memory location as free space So when there's new data to be saved it just uses this free space to save the data. So all of my file data is present in the memory but the system labeled them as free space. Now we need some kind of software that can read data labeled under free space and give us the data which is exactly what Recuva (https://www.ccleaner.com/recuva) from piriform does (There is much other software available). I've downloaded a free version and selected the directory to be recovered And here's the result - SUCCESS enter image description here
0

In my case, I accidentally discard the changes and then committed,

But, fortunately, I can able to find the files inside the bin.

Also the Undo (cmd+z) trick worked on un closed VS code window).

Soorya
  • 641
  • 9
  • 16
-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