Reverting a merge commit in Git
Sometimes, we need to merge two or more branches and then push the commit to the desired branch. But later we realize that we don’t need the merge in that repository, so the question here is how to undo or revert the merge commit that has already been pushed. There is nothing to worry about. Git has a great solution for this problem which we will discuss below.
There are two ways to handle this situation, depending on the problem; one is that it has not been pushed to the repository but has been committed. The other is that it has been committed and pushed to the repository. Both cases are discussed below:
Undoing unpushed Git commits
It is not possible to revert a commit directly in Git. Nevertheless, we need to check the commit before reverting. After reverting, the location of the working directory is now the same as the location of the working directory before we pushed the commit.
We can now apply git reset
the command to move the index pointer back to where it was before the commit. For example, if we want to keep the changes from commits Commit1
and Commite3
, but revert Commit2
the changes from commit , execute this command.
git reset HEAD~3
This command will return the index pointer to Commit2
where it was before the commit. After reverting the commit, we need to add the changed files to the working directory again. For example, execute the following command:
git checkout Commit1
git add Commit1
git checkout Commit3
git add Commit3
git commit
Afterwards, we can push commits Commit1
and Commit3
to the remote repository.
git push
If we want to merge two branches and then revert the changes from one branch, we can use the following command:
git merge --abort
This command is used to cancel the merge process. Afterwards, we can revert the changes to the branch. The commands discussed in this section are useful if we want to undo the effects of the last commit or multiple commits.
However, if we want to undo the effects of the last two or more commits, we need to reset the working directory to the state before the last commit. This is because it is not possible to directly revert one or more commits.
Reverting a pushed Git commit
In another case, if we have already pushed the merge commit to the remote branch and also pushed it, we have to make a new commit to revert 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 of the previous merge commit. However, this new commit will also contain all the changes of the original merge commit, so we need to edit the commit message to indicate that this is a revert commit. The options we mentioned -m 1
will tell Git that we want to keep the merged branch.
revert command
The revert command does not alter the working tree. It only modifies the index. revert command
It also has an option to restore the original message of the commit. In the example given above, -m
the revert option is used to restore the original message of the commit. The revert command can also be used to restore specific files from an integrated commit.
Git is a useful tool to use on our local branches when we make changes and decide that we no longer want to keep that change. revert
It is not recommended to use on public branches.
As we all know, git revert is completely reversible and it is impossible to undo its changes. If we perform git revert on a commit that is not the current commit on its branch, we have to be very careful because git revert will develop a new commit with a new commit ID.
If we later do a git reset --hard to revert the changes made by git revert, we will also remove the changes made by the developer who created the commit that git revert undoes. It is possible to use git revert on a public branch, but according to my opinion, it is not a good idea to do so. It is better to develop a new branch to make the changes and then merge it into master
the branch when you are done.
If we want to use git revert on a public branch, make sure we are the only ones working on that branch, otherwise we will undo everyone else's work.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Close the Git commit editor on Windows
Publish Date:2025/03/31 Views:62 Category:Git
-
In this article, we will discuss how to exit the Git commit editor. This can be a little tricky, especially if you are new to Git bash . Let's see how to exit the editor on Windows. Close the Git commit editor on Windows We will look at a t
Understand the Git commit sign-off function
Publish Date:2025/03/31 Views:160 Category:Git
-
In this article, we will explore the Git commit sign-off feature. This feature allows us to add a line containing our full name and email address to our commits. Signature confirming ownership and permission to submit submission. Understand
Add the file to the last commit in Git
Publish Date:2025/03/31 Views:176 Category:Git
-
This article outlines the process of adding a file to the last commit in Git. This comes in handy when you forgot to include a file in the last commit and don't want to create a new file. Let’s get straight to the point. Add the file to t
Git search commit messages using the command line
Publish Date:2025/03/31 Views:157 Category:Git
-
We can format git log the command to show commits with commit messages that match a specified pattern. This makes it easier when you want to find a commit but your repository has hundreds of commits. This article will discuss the process of
Git commit some files in one branch and make them available in another branch
Publish Date:2025/03/31 Views:194 Category:Git
-
This article shows how to commit specific files in one branch and make those files available in another branch. Suppose you have a project and you create a feature branch to make some slight modifications to the code. You modify and add new
Modifying a specific commit in Git
Publish Date:2025/03/31 Views:100 Category:Git
-
This article explains how we can modify a specific commit in Git. We may need to rename, compress, edit, or add files to a commit. The best way is to use the git rebase command in interactive mode . What do you think of this? Modifying a sp
Git overwrites Master with branch
Publish Date:2025/03/31 Views:79 Category:Git
-
Git is used to keep track of the source code we are working with; it also facilitates collaboration and helps us keep our projects in their current state. As we develop new features, their history should be at our fingertips as it is very h
Git Squash Commits
Publish Date:2025/03/31 Views:162 Category:Git
-
We will learn about Git squashing in this tutorial. The basic idea is to merge multiple consecutive commits into one. The main purpose is to compress many commits into a few related commits. Thus, doing so will make the git history look con
Merge remote branches into local branches in Git
Publish Date:2025/03/31 Views:134 Category:Git
-
This tutorial will merge a remote git branch into a local branch by cloning the remote repository and updating the changes locally. Merge remote branches into local branches in Git by cloning the remote repository and updating the changes l