Git merge is a process to merge two or more branches into a single branch. The current branch HEAD is the destination for the merge operation. Even merge is useful and regularly used for new features to merge the main development branch some cases may require reverting a Git Merge. In this tutorial, we examine how to revert the Git merge.
Undo Merge with “git reset”
Git provides different features and actions to work with different versions of the codebase. Git stores every state of the repository and objects and we can do anything we want like undo a merge. The
git reset command can be used to undo a merge by specifying the commit before the merge we want to revert back. In the following example, we undo merge by specifying the command like below.
$ git reset --hard 23abe32452
Alternatively, we can use the HEAD~1 pointer in order to specify the previous commit before the merge.
$ git reset --hard HEAD~1
Undo Pushed Merge with “git revert”
If the merge is pushed into the central repository the steps described above do not work. So after pushing the merge into the repository how can we revert the merge back. The
git revert command is used to revert merge to back and commit again. By using the following command the remote developers also revert back for the merge.
$ git revert -m 1 23abed625