简体   繁体   中英

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

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

Git commit

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 . 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 .

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.

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.

Explanation

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

And the git-reset man page describes the --hard flag thus:

--hard

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

(Use with caution!)

Here is a good introduction to how git-reset works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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