如何在`git rm abc.c`后恢复文件?

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

How to recover file after `git rm abc.c`?

gitgit-rm

提问by Anders Lind

I supposed to delete another file with git rm abc.c. But I deleted a wrong one. How can I recover it?

我应该删除另一个文件git rm abc.c。但是我删错了。我怎样才能恢复它?

Right now, when I issue git status, it says

现在,当我发出时git status,它说

deleted:   abc.c

BTW, I have other uncommitted changes now.

顺便说一句,我现在还有其他未提交的更改。

回答by Sunil D.

You need to do two commands, the first will "unstage" the file (removes it from the list of files that are ready to be committed). Then, you undo the delete.

您需要执行两个命令,第一个命令将“取消暂存”文件(将其从准备提交的文件列表中删除)。然后,您撤消删除。

If you read the output of the git statuscommand (after the using git rm), it actually tells you how to undo the changes (do a git status after each step to see this).

如果您阅读git status命令的输出(在 using 之后git rm),它实际上会告诉您如何撤消更改(在每个步骤之后执行 git status 以查看此内容)。

Unstage the file:

取消暂存文件:

git reset HEAD <filename>

git reset HEAD <filename>

Restore it (undo the delete):

恢复它(撤消删除):

git checkout -- <filename>

git checkout -- <filename>

回答by rob mayoff

First you need to reset the status of abc.cin the index:

首先,您需要重置abc.c索引中的状态:

git reset -- abc.c

Then you need to restore abc.cin your working tree:

然后你需要abc.c在你的工作树中恢复:

git checkout -- abc.c

回答by Gastón Saillén

If you accidentally did

如果你不小心做了

git rm -rf .

and removed all, you can recover it doing this.

并删除所有内容,您可以通过此操作恢复它。

git status
git reset HEAD .
git checkout -- .

first do git statusto see what files you have deleted. second reset the HEAD to unstage all the files with git reset HEAD .lastly, you can restore all the files by doing git checkout -- .

首先git status看看你删除了哪些文件。第二次重置 HEAD 以取消暂存所有文件,git reset HEAD .最后,您可以通过执行以下操作来恢复所有文件git checkout -- .