如何撤消 VS Code/Git 中的“放弃所有更改”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43541167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:56:28  来源:igfitidea点击:

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

gitversion-controlvisual-studio-code

提问by user1026622

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

我担心我已经知道答案了,但无论如何我都会在这里.. 我不小心点击了 VS Code (OSX) 中的“放弃所有更改”,现在一个月的工作都没有了。噗。问题是,我没有正确设置 GIT,所以我还没有做任何提交。无论如何我可以恢复我的工作吗?现在我的工作目录和空目录一样好..

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

[编辑] 我应该澄清一下,控制台日志说它做了一个“git clean -f -q”,这意味着文件已经消失了。但我想知道 VSCode 是否有备份、历史记录或缓存……或其他任何东西。我很绝望..

采纳答案by Marina Liu

Summary: Your work is lost, and cannot be restored.

摘要:您的工作已丢失,无法恢复。

The Clean All(discard all changes) option in VS code essentially executes:

VS 代码中的Clean All(丢弃所有更改)选项本质上执行:

git clean -fd
git checkout -- .

So it is git's hands whether git clean -fdcan undo discarding uncommitted changes. Unfortunately the changes are not stored in git since it not committed, so after git clean -fdis executed, git will remove untracked files by force.

因此,是否git clean -fd可以撤消丢弃未提交的更改是 git 的手。不幸的是,更改没有存储在 git 中,因为它没有提交,所以git clean -fd执行后,git 将强制删除未跟踪的文件。

After executing the command git checkout -- ., git will checkout all the modified files in git as the version of last commit.

执行命令后git checkout -- .,git会检出git中所有修改过的文件作为上次提交的版本。

回答by EFFG

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

在错误的文件上不小心按下了丢弃更改 btn 之后,我最终到了这里)没有找到恢复的方法,我在那个文件中尝试了简单的 CMD+Z(CTRL+Z),然后轰隆隆 - 一切都回来了,所以不要放弃并尝试:) 很高兴

回答by Eric Burnett

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.

如果您无法使用 Ctrl+Z 或 Command+Z 撤消或在垃圾箱/回收站中找不到您的文件,您可以使用文件恢复软件进行恢复。我能够使用 Piriform 的免费版 Recuva 来恢复我不小心丢弃的更改。

回答by roslyn

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.

我现在在我的 Windows PC 上的 GitHub 桌面客户端上做了同样的事情。我以为它会还原未提交的内容,但它决定还原所有内容。诶。

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

对我来说幸运的是,我检查了 Bin 并且所有丢弃的文件都在那里(以及我想首先删除的不需要的文件,后来我更小心地删除了,首先恢复这些文件)。

Not sure whether MacOS also moves them to Trash. Double check, you might be lucky ;)

不确定 MacOS 是否也将它们移至废纸篓。仔细检查,你可能很幸运;)

回答by Yuri

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.

就我而言,我错误地丢弃了分支中的更改。此代码已提交 bat 未推送。我去{projectPath}.git/logs/refs/heads/找到一个带有分支的文件。在这个文件中,我找到了标题列表。然后在控制台中,我git reset {commitHash}用来撤消提交。

回答by Nic_H

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.

我刚刚有这个问题。我按下了 VS Code 中错误文件旁边的“放弃更改”箭头(在添加和提交之前)。我以前的文件不在回收站中,经过研究,CTRL + Z / CMD + Z是唯一的出路。如果您已经关闭了该文件,那么您可能很不幸。

回答by Pulkit Aggarwal

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

我能够恢复我单击的文件discard all changes。毫无疑问,git 无法恢复那些丢失的更改,但我们的系统(和 VScode 有)。

How I recovered those changes?

我是如何恢复这些变化的?

  1. I just opened those files again in VScode, using ctrl+p.
  2. Once opened, then I pressed ctrl+zto try undo my changes, and guess what, My system's clipboard help me recover all of my exact changes.
  1. 我刚刚在 VScode 中再次打开这些文件,使用ctrl+p.
  2. 一旦打开,然后我按下ctrl+z尝试撤消我的更改,猜猜怎么着,我的系统剪贴板帮助我恢复了我所有的确切更改。

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

如果您正在处理一个新文件,那么它现在已被删除并且现在没有足迹。您仍然可以恢复文件,只需按ctrl+p,因为文件已经在 VScode 中打开,标签为(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 !!

简而言之,只要您没有重新启动 VScode,您仍然可以恢复您的更改。如果此解决方案也适用于您,请随时为我的答案投票。谢谢 !!