[Git] How to Undo When You Accidentally Execute git push origin master
I’ll introduce how to handle the situation when you accidentally execute git push origin master
.
Without overthinking it, just revert all the commits you want to undo, then push the master branch.
# 1. Revert all commits you want to undo
git revert e70e1149f59a7ebee10e34bcd0c42651bcedf08a
git revert 776eb2334228c42935b4d37b5ac61a0e78f956ef
# 2. Push to remote branch again after reverting
git push origin master
Since this assumes we’re dealing with a remote branch for a work product, I didn’t want to take risks, so I didn’t use force push like git push -f
.
Force push has the danger of further messing up the remote branch, so while the commit log gets dirty, I wanted to understand the method for when you want to take the safe approach.
That’s all from the Gemba.