DEV Community

Cover image for Revert git reset --hard πŸš‘ TIL
Andy Chong
Andy Chong

Posted on

Revert git reset --hard πŸš‘ TIL

Do note that this does not apply if you did not commit your changes before you did git reset --hard Thanks jessekphillips!

1 day, you are updating your branch...

git pull origin/branch_name
# got merge request? Hmmm... I wonder why πŸ€”
Enter fullscreen mode Exit fullscreen mode

And you saw that you have newer changes after merging it, compared to the origin.
So you want to just reset it to the same state as the origin, then you did

git reset --hard origin/branch_name
Enter fullscreen mode Exit fullscreen mode

.
.
.
OH MY GOD!!! 😱
You realize that you got the merge request because you actually committed some changes! God knows when 😡.

So here's the git command that gonna save you!

Introducing Git reflog!

git reflog

some other commits...
39386b35 HEAD@{8}: some commit message
18cb2ce7 HEAD@{9}: the commit that I want to revert back to!
f9386b35 HEAD@{10}: some commit message
some other commits...

git reset HEAD@{9}
Enter fullscreen mode Exit fullscreen mode

AND I just save your day! Just kidding, here's the answer that actually save our day!

Top comments (2)

Collapse
 
jessekphillips profile image
Jesse Phillips

I would like to be clear that --hard will in fact delete any uncommitted changes, and those changes are lost and won't show in reflog.

Collapse
 
andychongyz profile image
Andy Chong

Yes, you are right, thanks for the info!