我刚刚删除了一个星期的工作!如何撤消 git rm -r --cached?

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

I've just deleted one week of work! How to undo git rm -r --cached?

git

提问by Tyra

I commited a wrong file, so I wanted to clean it up, but accidentally I overwrote all my files in the directory with last files committed to git.

我提交了一个错误的文件,所以我想清理它,但不小心我用最后提交给 git 的文件覆盖了目录中的所有文件。

Help please!

请帮忙!

What I did:

我做了什么:

git add fileIdidnotwanttoadd
git rm -r --cached .
git reset --hard HEAD

result: All my fixes are gone! I fixed 3 very hard bugs and it's all gone!

结果:我所有的修复都消失了!我修复了 3 个非常困难的错误,一切都消失了!



Edit:

编辑:

Thank you all. I used most of your suggestions, still had to redo a few things, but all is restored now. No more perfectionism, I learned my lesson!

谢谢你们。我使用了你的大部分建议,仍然需要重做一些事情,但现在一切都恢复了。没有更多的完美主义,我吸取了教训!

采纳答案by sehe

(from: Recover from git reset --hard?)

(来自:从 git reset --hard 中恢复?

You cannotget back uncommitted changes in general, so the real answer here would be: look at your backup. Perhaps your editor/IDE stores temp copies under /tmpor C:\TEMP and things like that.[1]

一般来说,您无法恢复未提交的更改,因此这里的真正答案是:查看您的备份。也许您的编辑器/IDE 将临时副本存储在 /tmp或 C:\TEMP 下,等等。[1]

git reset HEAD@{1}

This will restore to the previous HEAD - in case you had something committed earlier

这将恢复到以前的 HEAD - 如果您之前提交了某些内容

[1]

[1]

  • vime.g. optionally stores persistent undo,
  • eclipse IDE stores local history;
  • vim例如可选地存储持久性撤消,
  • eclipse IDE 存储本地历史

such features might save your a**

此类功能可能会节省您的**

回答by VonC

  • If you did commit locally those bugfixes, you can recover them with git reflog.
    (for a single file, you can also try git rev-list)
  • If you didn't commit them (ie. they were only in the working tree, not the repo), they are gone from your local working tree.
  • If you did add them to the index (but not commit them), see Mark's answer.
  • 如果您确实在本地提交了这些错误修正,则可以使用git reflog.
    (对于单个文件,您也可以尝试git rev-list
  • 如果你没有提交它们(即它们只在工作树中,而不是在 repo 中),它们就会从你的本地工作树中消失。
  • 如果您确实将它们添加到索引中(但未提交它们),请参阅Mark回答

回答by Mark Longair

There are various situations in which you may be able to recover your files:

您可以在多种情况下恢复文件:

  1. If you staged your changes before doing the above commands (i.e. ran git add <file>when <file>contained the content you want to get back) then it should be possible to (somewhat laboriously) get that file back by following this method.
  2. If you ever created a commit that contained those files, then you can return to that commit by finding the object name of the commit in the reflog and then creating a new branch based on it. However, it sounds from the above as if you never committed those files.
  3. Supposing you ever ran git stashwhile working on those files, they are recoverable either via git stash list, or methods 1. or 2. above.
  1. 如果您在执行上述命令之前暂存更改(即git add <file><file>包含您想要取回的内容时运行),那么应该可以(有点费力地)按照此方法取回该文件。
  2. 如果您曾经创建过包含这些文件的提交,那么您可以通过在 reflog 中查找提交的对象名称然后基于它创建一个新分支来返回到该提交。但是,从上面听起来好像您从未提交过这些文件。
  3. 假设您曾经git stash在处理这些文件时运行过,它们可以通过git stash list,或者上面的方法 1. 或 2.恢复。

Otherwise, I'm afraid you may be out of luck. :(

否则,恐怕你会倒霉。:(

回答by Josh Albert

I did not find any of the above solutions successful. Running

我没有发现上述任何解决方案成功。跑步

git status

in the root you will see all the uncommitted changes. To discard those changes caused by the

在根目录中,您将看到所有未提交的更改。丢弃由

git -rm --cached <files>

you would want to use

你想用

git checkout -- *

I hope this helps alleviate your stress.

我希望这有助于减轻你的压力。