Git checkout——恢复丢失的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30643119/
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-19 11:05:17  来源:igfitidea点击:

Git checkout -- recover lost files

gitlocalgit-checkoutundo

提问by CodeWizard

I accidentaly deleted local file changes on git repository. They were NOT commited or even pushed.

我不小心删除了 git 存储库上的本地文件更改。他们没有承诺,甚至没有被推动。

What I did: git status(then files not staged for commit showed and I accidentaly removed whole folder called "smdr" by this comand): git checkout -- smdr

我做了什么: git status(然后显示未暂存的文件,我不小心删除了这个命令名为“smdr”的整个文件夹): git checkout -- smdr

Then files changes disappeared.

然后文件更改消失了。

How can I recover those files (birng everything back before that git checkout -- smdr comand)?

我怎样才能恢复这些文件(在 git checkout 之前恢复所有内容——smdr comand)?

回答by Matthieu Moy

You can't with Git. The files were not committed so they are not in history. You just got the (inexistant) version in the index with git checkout.

你不能用 Git。这些文件未提交,因此它们不在历史记录中。您刚刚在索引中获得了(不存在的)版本git checkout

Your only hope is your backup system.

您唯一的希望是您的备份系统。

回答by CodeWizard

You can use any of the given options:

您可以使用任何给定的选项:

Git reflog

Git 引用日志

Type git reflogand checkout the commit you need, it will "revert" your repository to the "deleted" commit.

输入git reflog并签出您需要的提交,它会将您的存储库“还原”为“已删除”的提交。

Git revert

Git恢复

Another option is use git revert SHA-1which will revert your commit. It will simply undo your changes

另一种选择是使用git revert SHA-1它会恢复您的提交。它只会撤消您的更改

Git reset

Git重置

Git reset will checkout the content of the given sha-1. It will set your branch to be at the same state as the SHA-1

Git reset 将检出给定 sha-1 的内容。它会将您的分支设置为与 SHA-1 处于相同状态