Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recovering added/staged file after doing git reset --hard HEAD^?

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?

I did look at a related question here: How can I undo git reset --hard HEAD~1? but, that question assumes that the one has done a Git commit.

like image 726
Susheel Javadi Avatar asked Jul 10 '09 10:07

Susheel Javadi


People also ask

How do I get my changes back after resetting git hard?

We can use the command git fsck to recover the files after a hard reset.

How do I recover a staged file in git?

Staged files are those which go into your next commit. If you accidentally added files to the staged area, you can undo this by typing git restore --staged <file> , so in this case, it would be git restore --staged lib.

Can you undo a reset hard git?

In most cases, yes. Depending on the state your repository was in when you ran the command, the effects of git reset --hard can range from trivial to undo, to basically impossible.


1 Answers

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

$ git fsck --cache --no-reflogs --lost-found --dangling HEAD 

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

Please read git fsck manpage.

like image 84
Jakub Narębski Avatar answered Sep 21 '22 03:09

Jakub Narębski