在GIT中撤消删除 [英] Undo delete in GIT

查看:133
本文介绍了在GIT中撤消删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一件非常愚蠢的事情。
我使用git commit(文件编辑+新文件)(C)提交了一个提交。
然后我做了修改最后一次提交。
然后我使用 git rm -r
递归删除所有文件(!)然后我做了另一个git commit(C)。

 
ABC

主人

有什么方法可以取消删除文件,但保留我在第一次提交时所做的更改? (C)
我宁愿不回(B)。
我尝试了git reset --soft head ^,然后git status列出了我删除的文件,然后我做了git checkout,但仍然没有运气。我甚至不知道它是否可能。

帮你一个忙,不要做 git checkout< hash> 就像其他答案所暗示的那样,并且会遇到更多问题。

如果您从工作目录中删除了文件并且未提交变化,做:

  git结帐-f 

小心:在执行此命令之前提交未提交的文件,否则您将全部丢失它们

删除的文件应该再次返回。



如果不是,并且您可以找到所需的提交(C等 - 您的问题不明确)从 git reflog ,只要做 git reset --hard< hash from reflog> ,你应该全部设置。


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).

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.

解决方案

Do yourself a favour and do not do git checkout <hash> like the other answer suggests and go into more problems.

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.

这篇关于在GIT中撤消删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆