0

I'm using Sourcetree with git and for some reason it decided to stage my changed files as to delete instead of just updating them. I didn't catch this error until after I committed and pushed. I then realized all my files were missing. Can I recover these files?

To be clear, I do not want to revert back to the previous commit's file version. I want to have the files in the states they were when git deleted them so my work is not wasted.

  • 2
    Possible duplicate of [Find and restore a deleted file in a Git repository](https://stackoverflow.com/questions/953481/find-and-restore-a-deleted-file-in-a-git-repository) – phd Jul 07 '18 at 21:39
  • @phd that will recover a file from a previous commit, but as noted I'm hoping there's a way to recover the file that had changes after that commit that was accidently deleted. – Disastercake Jul 07 '18 at 21:56
  • Git itself won't delete your work-tree files (well, except for `git clean`...), but it also does not *save* them anywhere. What it saves are staged files and committed files. Staged files that were not actually committed can sometimes be recovered via `git fsck --lost-found` (they show up as "dangling blob" and are copied into the lost+found dir, with hash IDs for names; you will have to reconstruct the names). If the content was never staged, you're out of luck. – torek Jul 07 '18 at 22:07
  • @torek I'm starting to wonder if Sourcetree GUI runs git clean when committing or pushing, because it definitely deleted the files (I never would have). Which would be a little scary... I'll have to look into that. – Disastercake Jul 10 '18 at 00:53

1 Answers1

0

Since you never commited the changes to git it cannot help you.

But most IDEs (assuming that you use one) have their own change history. E.g.: eclipse creates a "safepoint" each time you safe a file.

So you could recover the files state from the commit before the delete:

git checkout [commit_before_delete] path/to/your/file

And then used your IDE to get the latest saved version back. In eclipse this would be replace with local history where you get a list of available file versions labeled with the safe timestamp.

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51