如何恢复未暂存的更改

How to restore unstaged changes

我正在使用 Visual Studio 来处理 git-东西。而且我不知何故丢失了我想要恢复的更改。

这些是我执行的步骤:

1) 使用 VS:提交,文件错误:"WrongCommit"

我注意到提交是错误的,我只是想撤消它,这样我的项目再次领先于存储库并提供了提交的更改。

2) 使用 VS:单击还原:"RevertCommit"

现在 VS 中列出了两个提交:"WrongCommit" 和 "RevertCommit"。 并且本地项目突然重置为之前 "WrongCommit"的状态。所以我丢失了所有更改,但我认为它们仍必须包含在 "WrongCommit".

然后我找到了这个答案“Whosebug: Undo a commit and redo”:

$ git reset HEAD~                                          # (2)

答案指出(据我所知)命令“[..] 撤消提交并使您提交的更改未暂存(因此它们将显示为 "Changes not staged for commit [..]"。但是我现在没有任何更改要提交。

这是我使用的确切语句历史记录 (Image: Git Commands),您可以在其中看到 git 甚至说 'Unstaged changes after reset',但这些更改已经消失。

我怎样才能恢复这些更改?

运行 git reflog ScreenViewer 查找 WrongCommit 的提交哈希。假设是abc123。然后 运行:

git reset abc123 --hard
git reset HEAD~
git status
#modify files if necessary, and then git add the right files and git commit