10

VSCode 技巧:git discard all changes 补救措施

 2 years ago
source link: https://blog.dteam.top/posts/2022-04/how-to-restore-files-in-vscode-after-discarding-all-changes.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

VSCode 技巧:git discard all changes 补救措施

胡伟红 Posted at — Apr 24, 2022 阅读 10

一时手误,在 vscode 中,将工程下所有未提交的文件执行了 discard all changes,所有文件在 vscode、Finder、Trash 中都找不到了。

方法一:如果 vscode 中打开了所有文件,会显示文件已经删除,此时可以将文件另存。悲催的是,我关闭了所有文件。

方法二:git 对已经修改的文件会有缓存,但前提是之前提交过代码。我是一次都没有提交过。所以 git 没有缓存,看来 git 是指望不上了。

方法三:网上找到 vscode 有 Cache,但是在 Cache 目录下没有找到丢失的文件。

上述方法都行不通,仔细分析后,还是要从 vscode 下手。仔细看了 /users/yourname/Library/Application Support/Code/ 目录中的内容,发现 User 目录下有个 History 的目录,里面的文件夹更新的时间跟我改代码的时间接近,又看了下每个文件夹里面的内容,虽然文件名跟我的代码不一样,但是文件的扩展名一致。这些文件会不会就是代码的历史文件呢?

抱着死马当活马医的想法,随便在 /users/yourname/Library/Application Support/Code/User/History 目录下选了一个文件夹,把里面的所有文件都拷贝出来,用 vscode 打开,居然是我的某个代码文件的内容,而且是不同时间保存的文件内容。

看来每个目录对应一个文件。于是挨个将每个目录中的文件拷贝出来,找到最新的那个。通过这种方式将所有的文件都恢复了。虽然有些繁琐,但也是最快、损失最小的恢复代码的方式了。

事后,查看了 vscode 中对 History 的设置内容:

vscode 设置

如果不需要记录历史,可以 Disable 这个选项。幸运的是,我从来没有改过这个配置。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK