tags:

views:

54

answers:

4

I am a git noob and git just deleted a bunch of important files. How do I get them back?

I have a repo on my local machine. To get into git, I just right click my project folder and select "git bash here". This brings up the master where I do all my giting.

So I had some changes to stage and I did:

git add .

This staged a bunch of changes. I noticed that I didn't want some of these staged so I decided that I'd try to unstage everthing. I did this:

git reset --hard HEAD^

This basically deleted a bunch of files that I had made on the last commit and jumped to the commit before.

How do I get those files back? If I can't do it through git is there another way?

A: 

I stand corrected regarding my original advice (see edit history). As penance, I've tracked down the simplest way to recover your lost files from git:

git fsck --lost-found
ls .git/lost-found/other

Unfortunately, the filenames are lost, but the contents should all be there as distinct files.

Marcelo Cantos
He used `git add .` ... so he should be able to recover
tanascius
does fsck affect my repo at all? because I already did a:git reset --hard {removed head}to get back some of the changes. but i still need to get back my uncommited changes
Ryan
If you're at all concerned, just backup the entire repo first using something other than git. But I don't think fsck will damage anything.
Marcelo Cantos
A: 

No, with

git reset --hard HEAD^

the changes are reverted back to the last commit and lost permanently.

phoenix24
He used `git add .` ... so he should be able to recover
tanascius
A: 

From this SO-Question:


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.


tanascius
A: 

It sounds like you want the old HEAD (as opposed to HEAD^), in which case you can simply check out that revision. Use git reflog to show the previous HEADs and look for the commit you want, then check it out.

If you want the files that you staged before the reset, see the other answers.

Brian Cully
Good advice - of course the reflog will help to recover the last commit ... together with my fsck solution he should be able to restore all files.
tanascius