简体   繁体   English

如何恢复用“ git rm *”删除的文件?

[英]How to restore files deleted with “git rm *”?

Accidentally i deleted all files after 1st commit on local repo using git rm * . 偶然我使用git rm *在本地仓库上第一次提交后删除了所有文件。

Git commit 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/

I am unable to restore file on Ubuntu 14.10 ,git version 2.1.0 . 我无法在Ubuntu 14.10,git版本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 

    (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 . (此命令重置当前分支到当前提交的 )表示的母体fc9895a具有SHA 9b72b0c和消息first commit

Reset your branch to the desired state 将分支重置为所需状态

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. 由于您在运行git rm *之后尚未进行任何本地更改,因此您所要做的只是回到执行IF ELSE提交后存储库所在的状态。

Fortunately, you still have a record of the short SHA of that commit: fc9895a . 幸运的是,您仍然记录了该提交的短SHA: fc9895a Therefore, 因此,

git reset --hard fc9895a

is the command that will get you out of trouble. 是使您摆脱困境的命令。

Explanation 说明

git-reset is used to reset the current branch ( master , here) to the specified state ( fc9895a , here). git-reset用于将当前分支(此处为master )重置为指定状态(此处为fc9895a )。

And the git-reset man page describes the --hard flag thus: git-reset手册页描述了--hard标志,因此:

--hard

Resets the index and working tree. 重置索引和工作树。 Any changes to tracked files in the working tree since <commit> are discarded. <commit>以来,工作树中跟踪文件的任何更改都将被丢弃。

(Use with caution!) (请谨慎使用!)

Here is a good introduction to how git-reset works. 是一个很好的介绍git-reset工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM