问题
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..
回答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.
回答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
回答3:
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.
回答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 ;)
回答5:
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.
来源:https://stackoverflow.com/questions/43541167/how-do-you-undo-discard-all-changes-in-vs-code-git