6

I have accidentally forgot to "git add ." a bunch of files that I have created before creating a new branch using "git checkout -b name" and all my un-staged files have been replaced by the files from the head.

Is there any way of retrieving these files? They are not in stash nor they have been commited. Also I am not using an IDE which keeps a history of files.

I have read about "git fsck --lost-found", but before I try that, is there any other way I can revert the un-staged files back onto the filesystem?

Daniel Z.
  • 199
  • 1
  • 8
  • 1
    I don't quite understand what you did. Did you lose unstaged changes? Normally git tries quite hard to protect them. For example, if you run "git checkout -b name" with unstaged changes, git *will* preserve them. Could you give step-by-step instructions on how to reproduce your problem? – sleske Aug 21 '12 at 18:01
  • At the moment, I'm afraid your question is unlikely to get a good answer, because what you describe should not happen with git (and I could not reproduce it). – sleske Aug 21 '12 at 18:04
  • 1
    I cannot reproduce that issue on a new 'git init' myself. This is indeed a very strange issue. I did not loose unstaged changes, but essentially a folder that was representing a package and a source file that was inside of it. What I did exactly was: 1. At some point I cloned a repo, since then I have made a few commits to the staged files. 2. I have added some more files onto the file system without adding them to staging. 3. I have again performed a few commits on the staged files. 4. I created a new branch with checkout -b of the HEAD from the master branch and my unstaged files were gone. – Daniel Z. Aug 21 '12 at 22:35
  • I just accidentally ran `git reset --hard` instead of `--soft` and this definitely does remove unstaged changes. At first I thought that `git checkout` was to blame too because I switched branches immediately afterwards, but after checking my command history I realized what happened. For anyone in the same boat who ends up on this page, [this question](http://stackoverflow.com/questions/7374069/undo-git-reset-hard) might help. I was able to recover a few of my changes, not the important ones though :(. – Matt Kantor Mar 17 '13 at 20:50

1 Answers1

1

Unfortunately there is no way (not that I have found). Once I did something like that, but I was using Aptana Studio and could recover all the files.

Anyway, a good practice is to maintain your commits small. When you do that you'll never loose more than 30 minutes even with accidents like that.

hsgubert
  • 2,266
  • 1
  • 22
  • 23
  • Wait, what? I'm with @sleaske on this one. If Aptana's git integration is abandoning changes that git proper wouldn't, it's Aptana that's broken, not git. – Christopher Aug 21 '12 at 19:33
  • An interesting observation is that I have been using Eclipse and Git command line and at this point the lost package/files are still visible in Eclipse but you can't open it as there is an 'Error retrieving content description for resource /xxx/xxx/dashboard'. Also, the 'Restore from Local History' doesn't work, as I get a message informing me that there is no local history for this file. – Daniel Z. Aug 22 '12 at 08:19
  • 1
    Clarifing my answer. I was using Aptana as an editor but using `git` in the command line. Since Aptana maintains all tabs open (all the files that I had been editing) I just needed to save them again. – hsgubert Aug 22 '12 at 17:41