How do you undo "Discard all changes" in VS Code/Git

63,658

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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.

Solution 5

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

Share:
63,658
user1026622
Author by

user1026622

Updated on July 09, 2022

Comments

  • user1026622
    user1026622 almost 2 years

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

  • Shamseer Ahammed
    Shamseer Ahammed over 5 years
    what does this actually mean ? i dont undertand anything , can some body explain ,? is there any to undo uncommitted changes ?
  • onyeka
    onyeka over 5 years
    You just saved me some heartache. Thanks for this. Found in the trash for MacOS.
  • user1026622
    user1026622 about 5 years
    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).
  • Thomas Vuillemin
    Thomas Vuillemin about 5 years
    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.
  • user1026622
    user1026622 about 5 years
    @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.
  • Thomas Vuillemin
    Thomas Vuillemin about 5 years
    @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.
  • Yasser Jarouf
    Yasser Jarouf about 5 years
    I got stuck with 5k changes and vscode refused to discard changes. Hitting those commands did the trick!
  • PaxForce
    PaxForce almost 5 years
    THIS should be the accepted answer. You saved me a whole day's worth of work!
  • user1026622
    user1026622 over 4 years
    If it's comitted but not pushed, wasn't it possible to just use git push and git pull to get your files back?
  • Yuri
    Yuri over 4 years
    In may case it was only commit without push
  • GD- Ganesh Deshmukh
    GD- Ganesh Deshmukh almost 4 years
    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 )
  • TheBen
    TheBen almost 4 years
    @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 !!
  • MiraTech
    MiraTech over 3 years
    Note that with Recuva the recovered file may not have the same extension in my case file.cs~RF1d321e55.TMP
  • Travis Grannan
    Travis Grannan over 3 years
    I agree with PaxForce! This just saved me so much time! Thank you Eric!
  • kula85
    kula85 about 3 years
    you saved my life. :-)
  • Tamer Durgun
    Tamer Durgun almost 3 years
    Recuva just saved me here, thank you for let us remember that.