Search code examples
gitversion-controlvisual-studio-code

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


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


Solution

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