Unfortunately I did several times git reset --hard HEAD^
losing a quite big chunk of code in several files. Is there a way to restore those commits or in this case to forward where the HEAD was before, so I can bring up those lines that I lost?
Hard reset explained. In this case, you can restore the file using either git checkout or git reflog . You can find the hash-ID of the previous commit from the command: git log . In case you don't have the hash ID, you can use the command git reflog .
To hard reset files to HEAD on Git, use the “git reset” command with the “–hard” option and specify the HEAD. The purpose of the “git reset” command is to move the current HEAD to the commit specified (in this case, the HEAD itself, one commit before HEAD and so on).
Running git reset --hard ORIG_HEAD will let you go back to where you were, but it will discard your local changes, which you do not want. git reset --merge keeps your local changes.
So, to undo the reset, run git reset HEAD@{1} (or git reset d27924e ).
Use the reflog to recover the sha1 of the previous HEAD. In particular, the article reflog, your safety net will be particularly relevant to you. From that article:
The most common usage of this command is that you’ve just done a
git reset
and moved yourHEAD
back a few commits. But oops, you need that bit of code you left in the second commit. Crap. Now what?
Once you have found the sha1 of the commit you want to go back to, use something like:
git reset --hard 0a1b2c
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With