Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out the numbering of merge commit parents?

Tags:

git

merge

I need to do a git revert -m , but I have not been able to find any documentation on how the commit parents are numbered. Everything I've seen (including the help pages for rev-parse) just tells me that the parents are numbered but do not say how they are numbered. Could someone point out to me where this is defined and how to determine this?

like image 826
Ryan Avatar asked Aug 26 '11 22:08

Ryan


People also ask

How many parents does merge commits have?

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.

What are parents of a merge commit?

This new commit records both as its parents; the branch/commit you're merging in and the branch you are on when the merge occurs. The parent that gets recorded first (in the commit object of the new commit) is considered the "first parent", while the other one is considered the "second parent".

What is mainline parent number?

-m parent-number --mainline parent-number. Usually you cannot cherry-pick a merge because you do not know which side of the merge should be considered the mainline. This option specifies the parent number (starting from 1) of the mainline and allows cherry-pick to replay the change relative to the specified parent.

What is Parent 1 and Parent 2 in git?

Every commit in git has at least one parent (except for the first/initial commit). A commit's parent is the previous one. C1 is the initial commit. C2 is the second one.


1 Answers

git show --format="%P" <SHA>

will give you the parent SHAs of the given commit in numerical order. Most often the one you'll want to specify for git revert will be 1.

In the case of most merges, the SHA of the branch that was checked out will be the first parent, and the SHA of the branch that was merged in will be the second. (Basically, the checked out branch is always the first parent; the other parents are the branches that were specified to the merge command in the order of their specification.)

If you want to find the SHA for a particular parent, use the caret operator:

  • First parent: git rev-parse <SHA>^1
  • Second parent: git rev-parse <SHA>^2

et cetera.

like image 72
Amber Avatar answered Sep 25 '22 07:09

Amber