git 撤销删除的文件

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

git undo deleted files

gitgit-rmgit-stage

提问by Pradeep

I am new to git. I have checkout files from remote. I had to delete few files from the git repo. Instead of doing git rmcommand, I issued unix rm -rf foldercommand. I need to revert the delete command and then perform git rmcommand. How to revert to the latest code?

我是 git 新手。我有来自远程的结帐文件。我不得不从 git repo 中删除几个文件。git rm我没有执行命令,而是发出 unixrm -rf folder命令。我需要恢复删除命令,然后执行git rm命令。如何恢复到最新代码?

Note: I have not yet committed the staged files.The out out of git statusis the list of files deleted in the below format:

注意:我还没有提交暂存文件。输出git status是按以下格式删除的文件列表:

#   deleted:    i18n/angular-locale_sl.js
#   deleted:    i18n/angular-locale_in.js 

回答by VonC

I need to revert the delete command and then perform git rm command. How to revert to the latest code?

我需要恢复删除命令,然后执行 git rm 命令。如何恢复到最新代码?

Simply do a (since you haven't committed anything):

简单地做一个(因为你没有做任何事情):

cd /root/of/your/repo
git checkout HEAD -- .

That will restore the working tree to the index.

这会将工作树恢复到索引。

(A git reset --hardshould work too, but isn't needed here)

(A 也git reset --hard应该工作,但这里不需要)

But you could also register those deletion to the index directly:

但是您也可以直接将这些删除注册到索引中:

git add -A .

See "What's the difference between git add .and git add -u?"

请参阅“什么之间的区别git add .git add -u

回答by Damien ó Ceallaigh

If you have staged the deletion, first unstage it:

如果您已暂存删除,请先取消暂存:

git reset HEAD path/to/deleted/file

Then restore the file:

然后恢复文件:

git checkout path/to/deleted/file

回答by Sma Ma

Revert a git delete on your local machine

在本地机器上恢复 git delete

You're wanting to undo of git rm or rm followed by git add:

你想撤消 git rm 或 rm 后跟 git add:

Restore the file in the index:

恢复索引中的文件:

git reset -- <file>

git reset -- <file>

after that check out from index

之后从索引中签出

git checkout -- <file>

git checkout -- <file>

for example:

例如:

git reset -- src/main/java/de/foo/bar.java
git checkout -- src/main/java/de/foo/bar.java

回答by mockinterface

To supplement the answer by @VonC,

补充@VonC的答案,

I had to delete few files from the git repo. Instead of doing git rm command, I issued unix rm -rf folder command

我不得不从 git repo 中删除几个文件。我没有执行 git rm 命令,而是发出了 unix rm -rf 文件夹命令

The only difference between the two options is that git rmcan also remove the files from your git index. However, because you never added them to the index to begin with, there is no particular reason to use git rm, and you don't need to undo the plain /bin/rm.

这两个选项之间的唯一区别是git rm还可以从 git 索引中删除文件。但是,因为您从未将它们添加到索引中,所以没有特别的理由使用git rm,并且您不需要撤消普通的/bin/rm.