Reverting a merge commit

Move back to an older commit in Git using revertREVERTING A COMMITgit revert <commit-sha>The revert command in git takes in a commit id and compares the changes with the parent. The delta or the diff is calculated and the negation of it applied a…


This content originally appeared on Level Up Coding - Medium and was authored by Praveen Mathew

Move back to an older commit in Git using revert

REVERTING A COMMIT

git revert <commit-sha>

The revert command in git takes in a commit id and compares the changes with the parent. The delta or the diff is calculated and the negation of it applied as a new commit. In case the commit-sha is not specified, it is defaulted to the commit-sha of the HEAD commit.

Before Revert
$ git revert 9735432

This command creates a commit fbb1df5 on top of the HEAD negating the changes made by 9735432. The new HEAD will then point to the fbb1df5. The resultant tree will behave as if the commit 9735432 does not exist.

After Revert

THE MERGE COMMIT

Unlike other commits, the merge commit is a commit which has multiple (generally two) parents.

For instance, when a branch named feature is merged with master, a new commit is created on the branch master which has two parents, the previous head of master and the head of feature.

The branches before Merge

On merging the feature to master.

$ git checkout master
$ git merge feature

These commands create a new merge commit 1c32600.

The branches after merge

The merge commit 1c32600 has two parent commits

  1. 9735432 from master before the merge
  2. 1484b1a from branch feature

On running git show, the new commits display both the parents

$ git show
commit 1c32600da72208f8964da85fddb80e7dd43b15c9 (HEAD -> master)
Merge: 9735432 1484b1a

REVERTING THE MERGE COMMIT

While reverting the merge commit, the parent with which it needs to be compared should be specified. The parent may be specified with the -m flag in git revert followed by the parent-number.

The parent number is assigned from left. To revert the changes brought in by the feature branch, revert the commit with respect to the second parent (1484b1a).

$ git revert HEAD -m 2

This will revert all the changes made by the second branch (feature) on master. The resulting tree will behave as the branch feature was never merged to master.

git revert -m 2

Similarly, to revert the changes from the commits in the first parent of the merge commit run

$ git revert HEAD -m 1

This will revert all the commits that were made on the master after the feature branch was created leaving only the commits in the feature branch in the master.

git revert HEAD -m 1

In the above example,9745432 and b15b045 would be removed from the master branch leaving only the commits 1484b1a and 79fe70a and the older commits which are in feature.

Also, the master branch may be merged to feature branch.

$ git checkout feature
$ git merge master
Merge the master to feature branch

The changes in the commit in the 9745432 and b15b045 in the master branch will get included in the feature branch. This time for the merge commit the first parent is the original branch which is the commit 1484b1a itself whereas the commit in the master branch 9745432 is the second parent.

CAUTION

If the merge of master to the feature branch was unintentional. The correct way to undo it is to reset the branch. This can be done by running the following in the feature branch.

$ git reset HEAD~1

On the other hand, reverting a merge commit negates all the changes made by the branch of the specified parent.

For instance if after reverting the merge commit(177a8b) in the feature branch, it were merged back to master, it would wipe out the changes (9745432 and b15b045) made in the master branch.

Merging back after revert wipes out the changes

Now the master branch pointing to the commit a75ebc will not have the changes done in the commits 9745432 and b15b045.

Thanks for reading. Hope this was helpful. Please leave your comments if you have any questions or you feel something more needs to be added.


Reverting a merge commit was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Praveen Mathew


Print Share Comment Cite Upload Translate Updates
APA

Praveen Mathew | Sciencx (2021-04-24T11:53:12+00:00) Reverting a merge commit. Retrieved from https://www.scien.cx/2021/04/24/reverting-a-merge-commit/

MLA
" » Reverting a merge commit." Praveen Mathew | Sciencx - Saturday April 24, 2021, https://www.scien.cx/2021/04/24/reverting-a-merge-commit/
HARVARD
Praveen Mathew | Sciencx Saturday April 24, 2021 » Reverting a merge commit., viewed ,<https://www.scien.cx/2021/04/24/reverting-a-merge-commit/>
VANCOUVER
Praveen Mathew | Sciencx - » Reverting a merge commit. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/24/reverting-a-merge-commit/
CHICAGO
" » Reverting a merge commit." Praveen Mathew | Sciencx - Accessed . https://www.scien.cx/2021/04/24/reverting-a-merge-commit/
IEEE
" » Reverting a merge commit." Praveen Mathew | Sciencx [Online]. Available: https://www.scien.cx/2021/04/24/reverting-a-merge-commit/. [Accessed: ]
rf:citation
» Reverting a merge commit | Praveen Mathew | Sciencx | https://www.scien.cx/2021/04/24/reverting-a-merge-commit/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.