如何恢复用"git rm *"删除的文件? [英] How to restore files deleted with "git rm *"?

查看:392
本文介绍了如何恢复用"git rm *"删除的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

偶然地,我使用git rm *在本地存储库上第一次提交后删除了所有文件.

Accidentally i deleted all files after 1st commit on local repo using git rm * .

Git提交

git commit -m 'IF ELSE'
[master fc9895a] IF ELSE
 14 files changed, 3234 insertions(+)
 create mode 100644 Lecture 03 - Bootstrapping.ppt
 create mode 100644 Makefile
 create mode 100644 codegenerator.cpp
 create mode 100644 error.cpp
 create mode 100644 error.h
 create mode 100644 includelex.h
 create mode 100644 includeyacc.h
 create mode 100644 lexrule.l
 create mode 100644 rule.tab.c
 create mode 100644 stable.h
 create mode 100644 symboletable.cpp
 create mode 100644 test.c
 create mode 100644 yaccrule.y
 create mode 100644 yaccrule2.y

git rm *
rm 'Lecture 03 - Bootstrapping.ppt'
rm 'Makefile'
rm 'README.md'
rm 'codegenerator.cpp'
rm 'error.cpp'
rm 'error.h'
rm 'includelex.h'
rm 'includeyacc.h'
rm 'lexrule.l'
rm 'rule.tab.c'
rm 'stable.h'
rm 'symboletable.cpp'
rm 'test.c'
rm 'yaccrule.y'

我用来还原的命令序列.

Sequence of commands that i used to restore .

git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    Lecture 03 - Bootstrapping.ppt
    deleted:    Makefile
    deleted:    README.md
    deleted:    codegenerator.cpp
    deleted:    error.cpp
    deleted:    error.h
    deleted:    includelex.h
    deleted:    includeyacc.h
    deleted:    lexrule.l
    deleted:    rule.tab.c
    deleted:    stable.h
    deleted:    symboletable.cpp
    deleted:    test.c
    deleted:    yaccrule.y
    deleted:    yaccrule2.y

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .hg/


git reset --hard HEAD^1
HEAD is now at 9b72b0c first commit


git reset --hard HEAD
HEAD is now at 9b72b0c first commit


 git checkout
 Your branch is up-to-date with 'origin/master'.


 git reset HEAD test.c
 git reset HEAD  Makefile
 git reset HEAD --  Makefile

git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .hg/

我无法在Ubuntu 14.10 git版本2.1.0上还原文件. 有办法让我恢复文件吗?

I am unable to restore file on Ubuntu 14.10 ,git version 2.1.0 . Is there a way so that i can restore file?

推荐答案

侦探工作

根据对问题的描述,在遇到麻烦之前,您的仓库的历史记录必须包含两次提交:

Detective work

Based on your description of the problem, before running into trouble, your repo's history must have consisted of two commits:

9b72b0c first commit
fc9895a IF ELSE (HEAD, master)

证据

  • 有关第二次提交的信息位于

    Evidence

    • Information about your second commit is in the output of

      git commit -m 'IF ELSE'
      

    • The output of

      git reset --hard HEAD^1
      

      (此命令将当前分支重置为当前提交的 parent ),指示fc9895a的父级具有SHA 9b72b0c和消息first commit.

      (this commands resets the current branch to the parent of the current commit) indicates that the parent of fc9895a has SHA 9b72b0c and message first commit.

      由于在运行git rm *之后没有进行任何本地更改,因此您要做的就是回到执行IF ELSE提交后存储库所在的状态.

      Since you haven't made any local changes after running git rm *, all you have to do is go back to the state your repository was in right after you made the IF ELSE commit.

      幸运的是,您仍然有该提交的短SHA的记录:fc9895a.因此,

      Fortunately, you still have a record of the short SHA of that commit: fc9895a. Therefore,

      git reset --hard fc9895a
      

      是使您摆脱困境的命令.

      is the command that will get you out of trouble.

      git-reset用于将当前分支(此处为master)重置为指定状态(此处为fc9895a).

      git-reset is used to reset the current branch (master, here) to the specified state (fc9895a, here).

      git-reset手册页描述了--hard标志,因此:

      --hard

      重置索引和工作树.自<commit>以来,对工作树中跟踪文件的任何更改都将被丢弃.

      Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded.

      (请谨慎使用!)

      此处很好地介绍了git-reset有效.

      这篇关于如何恢复用"git rm *"删除的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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