DEV Community

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

Posted on

1

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!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

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!

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay