How to Revert a Git Merge With Conflicts

John Wachira Feb 15, 2024
How to Revert a Git Merge With Conflicts

This article illustrates reverting the git merge command when merge conflicts arise. We will also take a quick look at how you can undo a git merge that is successful and has been pushed to the remote repository.

Revert a Git Merge With Conflicts

In this section, we will employ an example as discussed below.

Our repository has the master and feature branches. We will edit the README.md file in both branches so that merging master onto feature results in conflict.

After running the git merge command, we should get something like that.

git merge

To undo such a merge, we can run the git merge --abort command, as shown below.

$ git merge --abort

This command will reset our repository to its prior state before the merge. It should restore even the uncommitted changes, although not reliably.

Besides, only newbies merge branches with uncommitted changes.

We can also use the git reset --merge command, which will do the same as the git merge --abort command.

$ git reset --merge

Another handy command is the git reset --hard. It will do away with the merge and any changes introduced to the working copy by the merge.

What if we want to undo a successful merge?

First, we will need the commit hash for the merge. We can run the git log --oneline command to list the commits in our repository.

We will then copy the hash for the merge commit, as shown below.

git log –oneline

We will use the git reset command with the commit’s hash after our merge commit. In simpler terms, the commit is at HEAD@{1}.

$ git reset --hard c315395
HEAD is now at c315395 Trial2

Alternatively, we can run the git reset command, as shown below.

$ git reset --hard HEAD~1

This will roll back our repository by one commit.

In a scenario where we had pushed the merge to the remote repository, how do we go about reverting the same?

If you’ve already pushed the changes, you must create a commit that reverts the merge changes and push them to the remote repository, as shown below.

$ git revert -m 1 08396d4

This will revert the changes introduced by the merge. We have used the git revert command with the commit hash of our merge commit.

Now we can push the changes to the remote to revert the merge.

In a nutshell, Git allows you to cancel a merge when conflicts arise. The git reset --merge and git merge --abort commands come in handy in such scenarios.

If your merge was successful and you have pushed the changes to the remote, you will have to create a new commit that reverses the changes introduced by the merge.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Reset

Related Article - Git Merge