DEV Community

Discussion on: Undo wrong Git changes

Collapse
 
balastrong profile image
Leonardo Montini

Might be worth mentioning that in case you have a pushed commit, you can still do the same as for local commit if you have force rights on the repo.

git reset --soft HEAD~
Enter fullscreen mode Exit fullscreen mode

And you get rid of the commit on your local branch, then

git push --force
Enter fullscreen mode Exit fullscreen mode

And the commit is also gone from the upstream repo as well.

There are a few different ways to get a similar result (get code back to the previous state), it's good to know as many as possible to properly decide which one to use based on the situation :)

Collapse
 
rahulrakesh16 profile image
Rakesh S M

A better approach but it can cause issues if multiple people are working on same branch. i guess it can be tweaked abit more.

Collapse
 
zhixiangyao profile image
Yao Zhixiang • Edited
git reset HEAD~
git checkout .
git push --force
Enter fullscreen mode Exit fullscreen mode
git reset --hard HEAD~
git push --force
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dailydevtips1 profile image
Chris Bongers

Thanks a lot Leonardo!
Indeed there are a lot of different option to keep track of so thanks for adding these 💖

If you want you can also adjust my original article to include these? *(leaving that up to you)

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

I'd add it but also explain in bold that this workaround with --force can also create issues with other team members commits if you're working on the same branch 😂

As you'll need to pull changes first
.. Won't be nice to find out a mate's commit disappeared suddenly 😅

I'd rather prefer to aim a single commit by it's hash identifier just to make sure.

Still double-checking before committing and using MR/PR process from an individual branch is king to avoid issues.

Best regards