How to undo or revert a merge in git

Merging branches in git

Merging branches in git is like, working on a copy of the code file without affecting the original code file, When the changes are done so we merge the branches into the main or master branch.  

Revert Merging

First, check the merged branch by executing the following command.

git checkout master

After checking the merged branch use the git reset command to undo or revert a merge in git we can use the following command.

git reset –hard

The git reset command is used to undo merging, actually, the get reset command checks the previous commit where branches did not merge.

We can use another command to undo a pushed merge in git.

git revert -m 1 <commit id here>

The git revert command is also used to undo or revert a pushed merge in git, if you have created a remote repository and pushed(uploaded) the project on GitHub so the git revert command will be helpful for you.

This command reverts all unwanted merges, it removes the previous commits from history this command is the best option for reverting a pushed merge in git. 

In git revert command “-m 1” tells a git not to touch the parent of the merging side.

Make sure to add the exact id number of a commit in the git revert command after -m 1, only that particular merge will be reverted whose commit id you will provide.

You can copy that id from your GitHub repository section.

Conclusion

In this article we discussed a short introduction to git and merging branches in git, then we discussed the commands for undoing and reverting a merge in git, we saw 2 different commands “git reset” and “git revert” and talk about what these commands actually do what tasks they perform.

Hope you like the tutorial if you have any queries regarding this article feel free to ask below, thanks for reading.

Suggested Article:

Leave a Comment