如何撤消“ git rm -r -f *“ [英] How to undo a " git rm -r -f * "

查看:518
本文介绍了如何撤消“ git rm -r -f *“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用git,
我有一个包含多个程序的目录,并执行以下步骤:


  1. 我做了 git add。

  2. 然后 git commit ,然后我得到了一个消息由于空的提交消息而中止提交。

  3. 然后我想,让我在通用消息下提交一组文件。所以我想删除所有添加的文件。

  4. 所以我做了 git rm -r -f

  5. 当我做一个 ls 我已经失去了我所有的代码。有没有什么办法可以让他们回来,我的愚蠢我甚至没有备份副本。

事情我已经跟踪到现在



我搜索了一些发现一些命令,但他们不工作


$ b $


致命的:错误版本'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. 程序
  5. em>可能能够从中止的提交中恢复文件

    基于@ the-malkolm的观察。



    从问题中的信息来看,没有任何提交,并且这些文件在任何时候都没有被跟踪。因为这样的git并不知道任何已被删除的文件。



    然而,这是有希望的。只是因为你在删除它们之前试图提交你的文件,所以你的文件中有一个幻影提交。



    下面是一个例子:

      $ git init 
    初始化/tmp/so/.git/
    $ echo中的空Git存储库find this text> README.md
    $ git add README.md
    $ git commit -v
    由于空提交消息而中止提交。
    $ git rm -rf。
    rm'README.md'
    $ git status
    #分支主

    #初始提交

    没有提交创建/复制文件并使用git add来跟踪)

    以上模拟问题中的事件,通常这是再次开始重写代码的时间。没有提交,文件不见了。



    确定中止提交



    然而,检查 .git repository会产生一些信息:

      $ tree .git / objects / 
    .git / objects /
    ├──91
    │└──9cdf847a6af7c655c8de1d101385f47f33e0f9
    ├──d6
    │└──7d51abe2521dcd00cec138b72f5605125c1e41
    ├──info
    └──pack

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

      $ git ls-tree 919cdf 
    致命错误:不是树形对象
    $ git ls-tree d67d51
    100644 blob 919cdf847a6af7c655c8de1d101385f47f33e0f9 README.md
    $

    第一个参考是代表README.md文件的 blob - 将会有在存储库中为每个文件创建一个blob,本例中的第二个是树引用。



    重新创建工作副本



    一旦树形散列已被识别,它就可以用来重建索引 read-tree

      $ git读取树d67d51abe2521dcd00cec138b72f5605125c1e41 
    $ git status
    #在分支上master

    #初始提交

    #要提交的更改:
    #(使用git rm --cached< file> ...未使用)

    #新文件:README.md

    #不会为commit提交更改:
    #(使用git add / rm< file> ...来更新将提交的内容)
    #(使用git checkout - < file> ...放弃工作目录中的更改)

    #deleted: README.md
    $

    目前,工作副本是空的,但丢失的文件进行提交阶段



    提交它们:

      $ git commit -mphew

    结帐符合存储库:

      $ git checkout。 
    $ ls
    README.md

    然后,所有文件都存在并拥有已经承诺。

    I was using git for the first time, I had a directory with several programs written in it and did the following steps

    1. I did git add .
    2. then git commit, then it i got a message Aborting commit due to empty commit message.
    3. Then i thought, let me commit group of files under common message. So i thought of removing all the added files.
    4. So i did git rm -r -f
    5. When i do a ls i have lost all my code. Is there any way that i can get them back, to my foolishness i don't have backup copy even.

    Things that i have followed till now

    I googled out some found some of the command but they are not working

    git stash if i type this command i get

    fatal: bad revision 'HEAD' fatal: bad revision 'HEAD' fatal: Needed a single revision You do not have the initial commit yet

    git reset HEAD, if i type this command i get

    Fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree. Use '--' to separate paths from revisions

    I really need to get those files back!

    Steps that i followed to create GIT

    1. mkdir BareRepo
    2. In BareRepo Directory i did git init, git status, git config --bool core.bare true
    3. Then i cloned the BareRepo git clone BareRepo/ Programs/
    4. In the Programs directory i did all the above said thing

    解决方案

    You may be able to recover the files from the aborted commit

    Based on the observation of @the-malkolm.

    From the information in the question, there are no commits, and the files were not tracked at any time. As such git does not really know about any of the files that have been deleted.

    That said however, there is hope. Simply because you attempted to commit your files before deleting them, there is a phantom commit floating around with all of your files in it.

    Here's an example:

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

    The above simulates the events in the question, and ordinarily this is the time to start rewriting the code again. There is no commit, the file is gone.

    Identify that aborted commit

    However inspecting the .git repository yields some information:

    $ tree .git/objects/
    .git/objects/
    ├── 91
    │   └── 9cdf847a6af7c655c8de1d101385f47f33e0f9
    ├── d6
    │   └── 7d51abe2521dcd00cec138b72f5605125c1e41
    ├── info
    └── pack
    

    There are objects in the git repository despite there being no commits. It's necessary to identify which of the two objects is the tree:

    $ git ls-tree 919cdf
    fatal: not a tree object
    $ git ls-tree d67d51
    100644 blob 919cdf847a6af7c655c8de1d101385f47f33e0f9    README.md
    $
    

    The first reference is the blob representing the README.md file - there will be one blob per file in the repository, the second in this example is the tree reference.

    Recreating the working copy

    Once the tree hash has been identified, it can be used to rebuild the index with 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
    $
    

    At this time, the working copy is empty but the lost files are staged for commit.

    Commit them:

    $ git commit -m "phew"
    

    And checkout to match the committed state of the repository:

    $ git checkout .
    $ ls
    README.md  
    

    And then, all files exist and have been committed.

    这篇关于如何撤消“ git rm -r -f *“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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