21

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!

Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
Tyra
  • 531
  • 1
  • 5
  • 16
  • 19
    Those bugs are going to be much easier to fix the 2nd time around. – Jesus Ramos Oct 03 '11 at 06:11
  • 4
    In the future, commit first, cleanup later. As long as changes are tracked by git (they've been committed) they can be recovered. If you don't commit, you could lost something. When in doubt, branch and commit! – Tekkub Oct 03 '11 at 06:38
  • 9
    Water under the bridge now but you might want to change your commit frequency to be hourly or daily io weekly. I can't imagine working for a week without having commited once. – Lieven Keersmaekers Oct 03 '11 at 07:26
  • 1
    @Tyra, please accept the answer you found most helpful by clicking the green tick on the left side of said answer. – davin Oct 03 '11 at 11:24
  • Just happened to me and hopefully the PyCharm IDE didn't re-indexed my code. – Stéphane Bruckert Jul 14 '14 at 18:44

4 Answers4

14

(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 e.g. optionally stores persistent undo,
  • eclipse IDE stores local history;

such features might save your a**

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • I don't know if you're still here, but let's assume I have vim persistent undo, and I accidently `git reset --hard` my stuff. Is there a way to recover from this ? – tforgione Nov 12 '15 at 10:26
  • Yeah. `git reset HEAD@{1}` and edit the file. Go back in the undo tree. I suggest to make a backup of the undodir – sehe Nov 12 '15 at 10:28
  • Yes, backup dir already done ! Thanks for the tip. I'll try your comment. Thanks ! – tforgione Nov 12 '15 at 10:28
  • What is `git reset HEAD@{1}` supposed to do ? And if when I edit the file, my undotree is empty, I guess I'm f***ed, right ? – tforgione Nov 12 '15 at 10:37
  • @DragonRock [`git reset HEAD@{1}`](http://stackoverflow.com/questions/5788037/recover-from-git-reset-hard/5788069#5788069) gets you back to revision before the --hard reset (if that was to current HEAD, you didn't need to. Luckily just doing `git reset HEAD@{1}` undoes it's own effect because of how `git reflog` works. If you don't have undo information, then, yes, it appears you have lost that. I don't know why. Perhaps you already lost it before making the backup? – sehe Nov 12 '15 at 10:40
  • Thanks, I think I might have lost some files before making the backup, but I think some should be ok. Will vim be able to find where the current version is in the undotree by himself ? – tforgione Nov 12 '15 at 10:43
  • @DragonRock I'd think so. I'm not actually sure. But it strikes me as odd if Vim would just /axe/ the undo tree if the file got externally edited. I suppose it should just record that as "ok, another change" – sehe Nov 12 '15 at 10:44
  • Thank you for the time you take to try to help me. I just saw [that](http://stackoverflow.com/questions/16975891/retain-undo-history-if-file-is-edited-outside-of-vim). Is this bad news for me ? – tforgione Nov 12 '15 at 10:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94916/discussion-between-sehe-and-dragonrock). – sehe Nov 12 '15 at 10:48
7
  • 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.
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • surely it's also worth mentioning that you can usually get back the content of files that have been staged but not committed, as [here](http://stackoverflow.com/questions/7374069/undo-git-reset-hard/7376959#7376959)? – Mark Longair Oct 03 '11 at 07:57
7

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

  1. If you staged your changes before doing the above commands (i.e. 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.
  2. 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.
  3. 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. :(

Community
  • 1
  • 1
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
0

I did not find any of the above solutions successful. Running

git status

in the root you will see all the uncommitted changes. To discard those changes caused by the

git -rm --cached <files>

you would want to use

git checkout -- *

I hope this helps alleviate your stress.

Josh Albert
  • 1,064
  • 13
  • 16