How to Revert Merge Commit in Git
Sometimes, we need to merge two or more branches and then push the commit to the desired branch. But then we realize that we don’t need that merge in that repository, so the question here is how to undo or revert a merge commit that is already pushed. Nothing to worry about. Git has an excellent solution for this problem which is discussed below.
There are two ways to deal with this situation depending on the problem; one is that it has not been pushed yet to the repository but has been committed. Another is that it is committed and pushed both onto the repository. both situations have been discussed below:
Revert Git Commit That Is Not Pushed
It is impossible to revert a commit in Git directly. Despite this, we need to check out the commit before reverting it. After reverting it, now the position of the working directory is the same as the position of the working directory before we pushed the commit.
We can now apply the git reset
command to move the index pointer back to its place,i.e. the position before the commit. For instance, if we want to keep the changes of the commits Commit1
and Commite3
but revert the changes of the commit Commit2
, execute this command.
git reset HEAD~3
This command will return the index pointer to the position before the commit Commit2
. After reverting the commit, we need to add the changed files to the working directory again. For example, execute the following commands:
git checkout Commit1
git add Commit1
git checkout Commit3
git add Commit3
git commit
After that, we can push the commits Commit1
and Commit3
to the remote repository.
git push
If we want to merge two branches and then revert the changes of a branch, we can use the following command:
git merge --abort
This command is used to cancel the merge process. After that, we can revert the changes to the branch. The commands discussed in this section are useful if we want to undo the effect of the last commit or several commits.
However, if we want to undo the effect of the last two or more commits, we need to reset the working directory to the state before the last commit. It is because it is impossible to revert one or more commits directly.
Revert Git Commit That Is Already Pushed
In the other case, If we have already pushed the merge commit to the remote branch and pushed it too, we have to make a new commit that reverts the changes. We will execute the following command:
git revert -m 1 <merge-commit-hash>
This will develop a new commit that will reverse the changes from the previous merge commit. However, this new commit will also contain all the changes from the original merge commit, so we will need to edit the commit message to indicate that this is a revert commit. Whereas the option that we have mentioned, -m 1
will tell Git that we want to keep the merged branch.
The revert command
does not change the working tree. It only modifies the index. The revert command
also has an option to restore the original message of the commit. In the example given above, the -m
option is used to restore the original message of the commit. The revert command can also be used to restore a specific file from the integrated commit.
Git revert is a useful tool to use on our local branches when we are going with a change and decide that we no longer want to keep that change. It’s not recommended to be used on public branches.
As we know, git revert is purely reversible, it’s impossible to undo its changes. If we execute git revert on a commit that is not the current commit on its branch, we must be very careful because git revert will develop new commits that will have new commit IDs.
If we later execute git reset –hard to revert the changes that git revert makes, we will also remove the changes made by the developer who created the commit that git revert undo. It is possible to use git revert on a public branch, but doing so is not a good idea as per my opinion. It is better to develop a new branch to make a change and then merge it into the master
branch when we are done.
If we want to use git revert on a public branch, be sure that we are the only one working on that branch, or we will be undoing the work of others.
Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.
LinkedInRelated Article - Git Revert
- How to Revert Commit Local in Git
- How to Revert a Git Repository to a Previous Commit
- Difference Between the Git Reset, Revert, and Checkout Commands
- How to Restore a Reverted Git Commit
- How to Delete Local Commits in Git