0

I'm an idiot. I started a new project and spent a few hours on it, then decided it should be in git. So I did a git init and then did a git add .. I then realized I hadn't copied a .gitignore so before commiting I wanted to go back and add that. Without thinking I did a git reset --hard and the obvious happened.

However - if i do a git fsck there are a load of dangling blobs, and a "missing tree".

> git fsck
notice: HEAD points to an unborn branch (master)
Checking object directories: 100% (256/256), done.
notice: No default references
dangling blob 04600b0b0185210bb56c8510538d8578b9451267
dangling blob 3b5f525f59d5fdf71a148d9bc04a80912006c5f7
dangling blob 3f4f89e0767381f0dae2c24b25abee3dffbca061
missing tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
dangling blob 504f47a8ce6bd4dc3bd1694da5ed3173efe17703
dangling blob 53c94aabbffadab697f9e689a0037999bf906032
etc...

So I can get all the files back - but is there a quick easy way to get them all back with filenames and in the right place using that missing tree?

git reset HEAD@{1} as suggested elsewhere only gives fatal: ambiguous argument 'HEAD@{1}': unknown revision or path not in the working tree.

MrPurpleStreak
  • 1,477
  • 3
  • 17
  • 28
  • Have you checked the solutions in [Recover from git reset --hard?](https://stackoverflow.com/questions/5788037/recover-from-git-reset-hard)? – Guildencrantz Dec 22 '16 at 20:59
  • Yes - but they all use reflog and have a previous commit it seems. `>git reflog show fatal: bad default revision 'HEAD'` is all I get :( – MrPurpleStreak Dec 22 '16 at 21:03
  • The accepted answer specifically addresses the case where you did a `git add` without a commit (`git fsck --lost-found`). – Guildencrantz Dec 22 '16 at 21:10
  • Thanks - I did try that and it didn't work. When I say no commits - I mean there isn't a single commit so there's no previous HEAD to restore to, which I guess it why it fails. – MrPurpleStreak Dec 22 '16 at 21:22
  • Just to check that I made a blank empty repo and re-tried what I did - `git reset HEAD@{1}` gives `fatal: ambiguous argument 'HEAD@{1}': unknown revision or path not in the working tree.` – MrPurpleStreak Dec 22 '16 at 21:26
  • A running in backgroudn backup system, like Time Machine on macOS/OSX or System Restore on Windows could help up to some point. – axiac Dec 22 '16 at 21:45
  • Well adding it to git from the outset (which I usually do) would have helped a great deal more! Not even the best backups can account for utter stupidity on my part. – MrPurpleStreak Dec 22 '16 at 22:17

1 Answers1

1

Since you never committed and never write-tree'd, the pathnames are gone.

jthill
  • 55,082
  • 5
  • 77
  • 137
  • I thought that may be the case. Luckily most of them are C# so the file name is obvious, and there aren't too many of them... thanks. – MrPurpleStreak Dec 22 '16 at 21:11