Manolo Manolo - 9 months ago 86
Git Question

Git - Undo pushed commits

I have a project in a remote repository, synchronized with a local repository (development) and the server one (prod). I've been making some commited changes already pushed to remote and pulled from the server. Now, I want to undo those changes. So I could just

git checkout
to the commit before the changes and commit the new changes, but I'm guessing that there will be problems to push them again to remote. Any suggestion on how should I proceed?

Answer Source

You can revert commits with git revert <commit_hash>. This will create a new commit which reverts the changes of the commit you specified with the <commit_hash>.

Note that you only revert that specific commit, and not commits after that. If you want to revert a range of commits, you can do it like this:

git revert <oldest_commit_hash>..<latest_commit_hash>

Just note that this command is a little bit funny. It actually doens't revert the commit specified with <oldest_commit_hash> itself, but the commits after that until and including <latest_commit_hash>.

Look at the git-revert man page for more information about the git revert command. Also look at this answer for more information about reverting commits.