Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I accidentally git reset one commit too far. How do I undo this?

Tags:

git

github

Quick preface, I have very little experience with git and other console based programs.

That being said, the problem at hand is that I used git reset --hard HEAD~ to roll back to a previous commit due to some corruption, but I ended up going one commit too far. Is there any way to undo the rollback or go forward a commit? I haven't synced it to the web yet.

like image 433
gamehen Avatar asked Sep 05 '26 16:09

gamehen


2 Answers

git reflog | head should show you the commit ids you need to recover your lost commit.

xxxxxxx HEAD@{0}: reset: moving to HEAD~
yyyyyyy Your lost commit?

You can try the following

git checkout yyyyyy -b newbranch
git log newbranch

or

git cherry-pick yyyyyy
like image 152
J.J. Hakala Avatar answered Sep 07 '26 10:09

J.J. Hakala


git reset HEAD@{1}

This takes you back to before you reset wrong, assuming only ONE commit too far. From there you can reset again to the correct commit.

git reflog

Use this to see a list of updates. Each update is assigned with a HEAD number (for example, HEAD@{1}: reset: blah..blah..), you use this number to point to where you want to go just like the first command.

like image 45
peanutz Avatar answered Sep 07 '26 11:09

peanutz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!