Monday, August 20, 2018

Significance of 1 and 2 while reverting merge commit

I came across a situation, where I need to revert one of my merge commit in git. I used the following command:

git revert -m 1 [merge commit id]

Now git provides one more option for the above command as below:

git revert -m 2 [merge commit id]

I did not get the difference between 1 and 2

References Used: https://www.christianengvall.se/undo-pushed-merge-git/

Solved

The git revert documentation provides the syntax like

git revert [--[no-]edit] [-n] [-m parent-number] [-s] [-S[]] …​

So the number after -m here represents the parent number

A merge commit may have two parents say consider an example that your current hash is 0ce2ca0b35f59af267241cf4d40d16a3e13ba6f3

and has two parents

df1acf5f54426d30f12c6b4558c3dd922297aae3
e19b912404ffd3c153ccac3072dbf22396896d2a

git revert -m 2 0ce2ca0b35f59af267241cf4d40d16a3e13ba6f3

will revert to e19b912404ffd3c153ccac3072dbf22396896d2a

Documentation


A git repository is just a tree of commits. A merge commit is just a commit that has two (or more) parents.

If you revert a commit with a single parent, you're just reversing the changes introduced by that commit.

If you're reverting a merge commit, however, you need to revert all the changes from the branch that you merged in. However, there's no real concept of which branch was merged to which - you just have the parents of the commit, and -m tells git which parent you want to revert to (ie. undo the changes introduced from the other parent)

See the documentation here: https://git-scm.com/docs/git-revert#git-revert--mparent-number


No comments:

Post a Comment