Revert commit on remote branch without force pushing

bpoiss picture bpoiss · Mar 9, 2016 · Viewed 7.4k times · Source

How can I revert some remote repository to an old commit without force pushing and losing history? I know I could do

git reset --hard <commit-hash>
git push -f origin master

but I don't have permissions to force push and I also don't want to lose the history.

I want to do this because I pushed some **** into my master branch and the automatically deployment deploys to production environment from this branch. I worked around it by creating a new branch and changed the deployment settings to use this one, but is there any option to just set the current state of the remote repository to an old commit in case this happens?

Answer

Enrique Quero picture Enrique Quero · Mar 9, 2016

You can use git revert <commit>… for all the n commits, and then push as usual, keeping history unchanged.

https://git-scm.com/docs/git-revert