Git Undoing Revert some existing commits


Example

Use git revert to revert existing commits, especially when those commits have been pushed to a remote repository. It records some new commits to reverse the effect of some earlier commits, which you can push safely without rewriting history.

Don't use git push --force unless you wish to bring down the opprobrium of all other users of that repository. Never rewrite public history.

If, for example, you've just pushed up a commit that contains a bug and you need to back it out, do the following:

git revert HEAD~1
git push

Now you are free to revert the revert commit locally, fix your code, and push the good code:

git revert HEAD~1
work .. work .. work ..
git add -A .
git commit -m "Update error code"
git push

If the commit you want to revert is already further back in the history, you can simply pass the commit hash. Git will create a counter-commit undoing your original commit, which you can push to your remote safely.

git revert 912aaf0228338d0c8fb8cca0a064b0161a451fdc
git push