How to Undo Pushed Git Commits

One of the frequent problems that can happen to developers is having pushed changes to the remote git repository, but then wishing to undo those changes and make new ones. Also, check out How to Revert a Git Repository to a Previous Commit for additional information.

Watch a course Git & GitHub - The Practical Guide

Reverting commits

Revert individual commits with the git revert command:

git revert <commit-hash>

Running the command will create a new commit that reverts the changes of the specific git commit. It will only revert the specific commit, not the commits coming after it. Note that the commit-hash mentioned here is a unique id called hash or SHA. the hash is identical for every commit. For reverting a range of commits, run the following:

git revert <oldest-commit-hash>..<latest-commit-hash>

This will revert the commits between the given range.

The git revert Command

The git revert command is considered as an undo command and reverts the changes introduced by the commit and adds a new commit with resulting reversed content. This is essential because it doesn’t allow losing history. Reverting is used for applying the inverse commit from the project history and help automatically go back and make fixes. Similar to git checkout and git reset, git revert also takes a specific commit, but it does not move reference pointers to this commit.