如何撤消"git rm -r -f*"

Man*_*anu 4 git git-rm

我第一次使用git,我有一个目录,里面写着几个程序并执行了以下步骤

  1. 我做到了 git add .
  2. 然后git commit,我收到了一条消息Aborting commit due to empty commit message.
  3. 然后我想,让我在常见消息下提交一组文件.所以我想删除所有添加的文件.
  4. 所以我做了 git rm -r -f
  5. 当我这样做时,ls我丢失了所有代码.有什么方法可以让他们回来,我的愚蠢我甚至没有备份副本.

到目前为止我所遵循的事情

我用Google搜索了一些发现的命令,但是它们没有用

git stash 如果我输入这个命令我得到

致命:糟糕的修改'HEAD'致命:糟糕的修订'HEAD'致命:需要单一修订您还没有初始提交

git reset HEAD,如果我输入这个命令我得到

致命:模糊的论点'HEAD':未知的修订或路径不在工作树中.使用" - "将路径与修订分开

我真的需要把这些文件拿回来!

我创建GIT时遵循的步骤

  1. mkdir BareRepo
  2. BareRepo目录我做git init,git status,git config --bool core.bare true
  3. 然后我克隆了BareRepo git clone BareRepo/ Programs/
  4. Programs目录中我做了以上所说的所有事情

AD7*_*six 6

可以从中止的提交中恢复文件

根据对@ the-malkolm的观察.

根据问题中的信息,没有提交,并且不会随时跟踪文件.因为这样的git并不真正知道任何已被删除的文件.

然而,那说,有希望.只是因为你试图在删除它们之前提交文件,所以有一个幻像提交在其中包含所有文件.

这是一个例子:

$ git init
Initialised empty Git repository in /tmp/so/.git/
$ echo "find this text" > README.md 
$ git add README.md 
$ git commit -v 
Aborting commit due to empty commit message.
$ git rm -rf .
rm 'README.md' 
$ git status
# On branch master
# 
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
Run Code Online (Sandbox Code Playgroud)

上面模拟了问题中的事件,通常这是重新开始重写代码的时间.没有提交,文件消失了.

确定中止提交

但是,检查.git存储库会产生一些信息:

$ tree .git/objects/
.git/objects/
??? 91
?   ??? 9cdf847a6af7c655c8de1d101385f47f33e0f9
??? d6
?   ??? 7d51abe2521dcd00cec138b72f5605125c1e41
??? info
??? pack
Run Code Online (Sandbox Code Playgroud)

尽管没有提交,但git存储库中有对象.有必要确定两个对象中的哪一个是:

$ git ls-tree 919cdf
fatal: not a tree object
$ git ls-tree d67d51
100644 blob 919cdf847a6af7c655c8de1d101385f47f33e0f9    README.md
$
Run Code Online (Sandbox Code Playgroud)

第一个引用是表示README.md文件的blob - 存储库中每个文件将有一个blob,本例中的第二个是树引用.

重新创建工作副本

一旦识别出树形哈希,它就可以用于使用read-tree重建索引:

$ git read-tree d67d51abe2521dcd00cec138b72f5605125c1e41
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   README.md
#
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    README.md
$
Run Code Online (Sandbox Code Playgroud)

此时,工作副本为空,但丢失的文件将暂存以进行提交.

提交他们:

$ git commit -m "phew"
Run Code Online (Sandbox Code Playgroud)

并且checkout匹配存储库的已提交状态:

$ git checkout .
$ ls
README.md  
Run Code Online (Sandbox Code Playgroud)

然后,所有文件都存在并已提交.

  • 如果有任何东西被添加到临时区域,那么它仍然在回购中,直到运行git-gc. (3认同)