How to reset remote to an older commit?

Resetting the remote to an older commit can be done in multiple ways. If we don't want to rewrite the git history (git log) then we can do it the following ways:

Without rewriting the git history

Using git revert

Lets assume we are on master branch and we have following commits:
A --> B  --> C 
The last commit is C. The scenario is the last pushed commit C is junk and we want reset our master to the older commit B. We not only want to reset our remote to commit B but also want to keep the git history. Even if someone else pulls our junk commit C still we will be fine. We can use git revert to do this. See the command below:
git revert --no-commit C  
git commit -m 'commit message'
git push origin master

Using git checkout

Using git checkout we can do it very nicely. If we can use the git checkout with the following options then it will keep the git history. See the commands below:
git checkout -f A -- .
git commit -m 'commit message'
git push origin master

By rewriting the git history

If we are not concerned (not standard) about the git history and nobody else pulled the junk commit C, then we can simply reset the master to an older commit using git reset. Afterwards we can simply push the master. See the code below:
 git reset --hard A
 git push -f origin master

Comments