I've just deleted one week of work! How to undo git rm

I commited a wrong file, so I wanted to clean it up, but accidentally I overwrote all my files in the directory with last files committed to git.

Help please!

What I did:

git add fileIdidnotwanttoadd
git rm -r --cached .
git reset --hard HEAD

result: All my fixes are gone! I fixed 3 very hard bugs and it's all gone!


Edit:

Thank you all. I used most of your suggestions, still had to redo a few things, but all is restored now. No more perfectionism, I learned my lesson!


(from: Recover from git reset --hard?)

You cannot get back uncommitted changes in general, so the real answer here would be: look at your backup. Perhaps your editor/IDE stores temp copies under /tmp or C:TEMP and things like that.[1]

git reset HEAD@{1}

This will restore to the previous HEAD - in case you had something committed earlier

[1]

  • vim eg optionally stores persistent undo,
  • eclipse IDE stores local history ;
  • such features might save your a**


    There are various situations in which you may be able to recover your files:

  • If you staged your changes before doing the above commands (ie ran git add <file> when <file> contained the content you want to get back) then it should be possible to (somewhat laboriously) get that file back by following this method.
  • If you ever created a commit that contained those files, then you can return to that commit by finding the object name of the commit in the reflog and then creating a new branch based on it. However, it sounds from the above as if you never committed those files.
  • Supposing you ever ran git stash while working on those files, they are recoverable either via git stash list , or methods 1. or 2. above.
  • Otherwise, I'm afraid you may be out of luck. :(


  • If you did commit locally those bugfixes, you can recover them with git reflog .
    (for a single file, you can also try git rev-list )
  • If you didn't commit them (ie. they were only in the working tree, not the repo), they are gone from your local working tree.
  • If you did add them to the index (but not commit them), see Mark's answer.
  • 链接地址: http://www.djcxy.com/p/4020.html

    上一篇: 如何在我执行git重置后恢复提交

    下一篇: 我刚刚删除了一周的工作! 如何撤消git rm