Revert a merge commit in Git that's already pushed to remote
Git is an important tool for managing projects. It would be bad if there weren’t a way to undo things, for example, commits. This is because we often make mistakes while building.
Here’s a guide on how to revert a merge commit in Git that’s already pushed to the remote.
Let’s get down to it.
Revert a merge commit in Git that’s already pushed
Reverting a merge commit that’s already pushed to Remote is easy. There are simply two Git commands that we’ll use:
- The
Git logcommand: Thegit logis used to pull up project history in Git. It makes it easy to see our changes and helps debug. To see our project history, run the following command:
git log
- The
Git revertcommand: Thegit revertis used for undoing things in Git. It is used to undo changes of a commit in a git repository. It takes a particular commit and reverses its changes but will not return your project to its previous state. Run it with the hash of the particular commit you want to revert.
git revert git-commit-hash
How to revert a merge commit
- Step 1: Get the history of your project. Getting the history is made to get the hash of the commit we wish to revert. Run
git log.
The result looks like this:
commit 465006cef583dac8a01855a996f95b321a208f7e
Merge: 675a7e5 76db734
Author: Akande Olalekan Toheeb <akandeoalalekantoheeb9@gmail.com>
Date: WED NOV 16 19:30:24 2020 +0100 Merge branch 'GH-pages' Conflicts: README
Note: Merge Commits have two parent commits. Therefore, there will be two hashes of the merge commit.
-
Step 2: Revert the merge commit using the
git revertcommand.Since it’s a merge commit, you run the
git revertcommand with either of-m 1or-m 2option.
git revert git-commit-hash -m 1
Note:
-m 1instruct Git to revert the first parent of the merge commit while-m 2reverts the second.
Free Resources