views:

318

answers:

3

I added a new file F1 and made changes to another file F2 but then did a "git reset --hard HEAD^" and I have lost all the changes to the files.

Is there SOME way, I can get them back.

Please help.

Thanks in advance.

ps: I did look at a related question here: http://stackoverflow.com/questions/5473/undoing-a-git-reset-hard-head1 but, that question assumes that the one has done a git commit.

+1  A: 

Unfortunately, if you haven't created a commit (which includes creating a stash) then Git doesn't know anything about those files and therefore will not have created any blobs that reflect their previous contents.

git reset --hard is a dangerous command. Hopefully you can recreate the work.

Greg Hewgill
This isn't quite true, you don't have to make a commit. As soon as you do a git add or a git stage on a file, git will place a copy of the file in its object database. It may not be referenced, but it will stay around until garbage collected.
Charles Bailey
That's true and a good point. However, stashing a file really does create a low-level commit object under the covers. I'm not sure whether adding does; the index might be the special case.
Greg Hewgill
Stashing makes one (or two) commit objects, adding doesn't. It's basically just a hash object and update index. There are no new tree or commit objects created in the object database.
Charles Bailey
Good to know, thanks for the clarification.
Greg Hewgill
A: 

Actually, if you've added the object to the index (by using git add), there is a blob created for that state of the object - but there is no tree (and thus, commit) object that is referring to it. This is how one gets a 'dangling' loose object file, and if you run git fsck it will show you the unreferenced blob (git gc will delete these types of objects if it is run).

Because of this, you can use the reflog, if you have it enabled, to try and restore the index state for your file F1 that has been added. If you haven't added F2 at all, then as Greg said, git doesn't know anything about it and you're out of luck there.

Matt Enright
+4  A: 

You can (with some work) recover state of file at the last "git add <file>". You can use

$ git fsck --cached --no-reflogs --lost-found --unreachable HEAD

and then examine files in '.git/lost-found/blob/' directory.

Please read git fsck manpage: I have not checked above invocation.

Jakub Narębski