Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

restore - git reset --hard HEAD^

Tags:

git

commit

reflog

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?

like image 579
Lan Avatar asked Jul 15 '10 22:07

Lan


People also ask

How do I restore git After resetting hard?

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 .

How do I reset git back to head?

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).

What does git reset -- hard head do?

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.

How do I undo a reset head?

So, to undo the reset, run git reset HEAD@{1} (or git reset d27924e ).


1 Answers

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 your HEAD 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
like image 163
Greg Hewgill Avatar answered Sep 28 '22 19:09

Greg Hewgill