How to Undo Pushed Commits

One of the frequent problems that can happen to developers is that they have pushed changes to the remote Git repository, but then they want to undo these changes and make new ones. Also, check our How to Revert a Git Repository to a Previous Commit for additional information.

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. For reverting a range of commits, run the following:

git revert <oldest_commit_hash>..<latest_commit_hash>

This will revert the commits between and including those that are specified. However, it won’t create any commit with the reverted changes. The revert only changes the working tree and the staging area.

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.


Do you find this helpful?

Related articles