How to Revert a Merge Commit Already Pushed to the Remote Branch
Imagine you have pushed your local branch to a remote using the git push command and then found out that one of the commits should be reverted. You can fix it. Here is a solid strategy on how to do it.
When you view your merge commit in the output of the git log command, you can see its parents on the Merge line. It will look like this:
commit 4f937c683929b08379097828c8a04350b9b8e45 Merge: 7676ee5 8c4b635 Author: SSL Date: THU JAN 21 19:30:24 2020 +0100 Merge branch 'gh-pages' Conflicts: README
In this event, you are recommended to use the git revert command like this:
git revert 8f937c6 -m 1
It will give you the tree as it was in:
In case you run:
git revert -m 2
The tree will be reinstated like it was in:
For the better understanding of parent IDs, you need to invoke:
git log 7676ee5
git log 8c4b635
Usage of the git revert Command¶
The git revert is mainly used for undoing changes to a commit history of the git repository. This command takes a particular commit, but it doesn’t move ref commits to that commit. The git revert operation takes the particular commit, inverses its changes, and generates a new “revert commit”.
But you should also consider that git revert can undo a single commit and will not return to the previous state of your project. That operation can be done with the help of the git reset command.
Definition of Git Log¶
Whenever it is necessary to list and filter your project history or to search for specific changes, you can turn to the git log command. It will show you the committed snapshots. Note that this command can work merely on the committed history, in comparison with git status, which controls the working directory, as well as the staging area.