在 GIT 中撤消删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9477702/
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
Undo delete in GIT
提问by Nately
I made something very stupid.
I made a commit using git commit (file edits + new files) (C).
Then I made amend last commit.
Then I deleted all files recursively (!) using git rm -r
Then I made another git commit (C).
我做了一件非常愚蠢的事情。我使用 git commit (file edits + new files) (C) 进行了提交。然后我修改了最后一次提交。然后我递归地删除所有文件(!)git rm -r
然后我做了另一个 git commit (C)。
A-B-C ↑ master
Is there any way to undelete the files but keep the changes I had in my first commit? (C) I'd rather do not go back to (B). I tried git reset --soft head^, so then the git status lists files I deleted, then I did git checkout, but still no luck. I don't even know if it's possible.
有什么方法可以取消删除文件但保留我在第一次提交时所做的更改?(C) 我宁愿不回到 (B)。我尝试了 git reset --soft head^,然后 git status 列出了我删除的文件,然后我做了 git checkout,但仍然没有运气。我什至不知道这是否可能。
回答by manojlds
Do yourself a favour and do not do git checkout <hash>
like the other answer suggests and go into more problems.
帮自己一个忙,不要git checkout <hash>
像其他答案建议的那样去做,然后再去解决更多的问题。
IF you have deleted file from your working directory and not committed the changes yet, do:
如果您已从工作目录中删除文件但尚未提交更改,请执行以下操作:
git checkout -f
CAUTION: commit uncommitted files before executing this command, otherwise you're going to lose them all
注意:在执行此命令之前提交未提交的文件,否则您将丢失所有文件
The deleted files should be back again.
删除的文件应该会再次返回。
If not and if you can find the commit that you want ( C, etc. - your question is not clear ) from git reflog
, just do git reset --hard <hash from reflog>
and you should be all set.
如果没有,并且如果您可以从 中找到您想要的提交(C 等 - 您的问题不清楚)git reflog
,只需执行即可git reset --hard <hash from reflog>
,您应该已准备就绪。
回答by Sascha
If I understood you correctly you rewrote commit C. So the original commit, let's call it C1 is not accessible from your commit graph, but it is still there (git keeps all commits for a while). Use git reflog
to get the commit hash and git checkout <hash>
or another appropriate command to get to the old state C1.
如果我理解正确,你重写了提交 C。所以原始提交,我们称之为 C1 不能从你的提交图中访问,但它仍然存在(git 保留所有提交一段时间)。使用git reflog
得到提交哈希和git checkout <hash>
或其他适当的命令去老态C1。