Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I undo git reset --hard HEAD~1?

Is it possible to undo the changes caused by the following command? If so, how?

git reset --hard HEAD~1 
like image 258
Paul Wicks Avatar asked Aug 07 '08 23:08

Paul Wicks


People also ask

How do I undo git reset HEAD 1?

So, to undo the reset, run git reset HEAD@{1} (or git reset d27924e ). If, on the other hand, you've run some other commands since then that update HEAD, the commit you want won't be at the top of the list, and you'll need to search through the reflog .

How do I get my git head back?

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 after git reset hard?

If you do git reset --hard <SOME-COMMIT> then Git will: Make your current branch (typically master ) back to point at <SOME-COMMIT> . Then make the files in your working tree and the index ("staging area") the same as the versions committed in <SOME-COMMIT> .


1 Answers

Pat Notz is correct. You can get the commit back so long as it's been within a few days. git only garbage collects after about a month or so unless you explicitly tell it to remove newer blobs.

$ git init Initialized empty Git repository in .git/  $ echo "testing reset" > file1 $ git add file1 $ git commit -m 'added file1' Created initial commit 1a75c1d: added file1  1 files changed, 1 insertions(+), 0 deletions(-)  create mode 100644 file1  $ echo "added new file" > file2 $ git add file2 $ git commit -m 'added file2' Created commit f6e5064: added file2  1 files changed, 1 insertions(+), 0 deletions(-)  create mode 100644 file2  $ git reset --hard HEAD^ HEAD is now at 1a75c1d... added file1  $ cat file2 cat: file2: No such file or directory  $ git reflog 1a75c1d... HEAD@{0}: reset --hard HEAD^: updating HEAD f6e5064... HEAD@{1}: commit: added file2  $ git reset --hard f6e5064 HEAD is now at f6e5064... added file2  $ cat file2 added new file 

You can see in the example that the file2 was removed as a result of the hard reset, but was put back in place when I reset via the reflog.

like image 61
Brian Riehman Avatar answered Nov 14 '22 07:11

Brian Riehman