How to Undo Pushed Git Commits
Read the tutorial and learn the solution to one of the most frequent questions concerning the method of reverting already pushed commit with simple command.
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.
Reverting commits
Revert individual commits with the git revert command:
git revert individual commits
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 identifier called a hash or SHA. Each commit has a distinct hash. For reverting a range of commits, run the following:
git revert a range of commits
git revert <oldest-commit-hash>..<latest-commit-hash>This will revert the commits between the given range.
The git revert Command
The <kbd class="highlighted">git revert</kbd> command is considered an undo command that reverts the changes introduced by a commit and adds a new commit with the resulting reversed content. This is essential because it preserves project history. Reverting is used to apply the inverse of a commit from the project history and helps automatically go back to make fixes. Similar to git checkout and git reset, <kbd class="highlighted">git revert</kbd> also takes a specific commit, but it does not move reference pointers to this commit.