Ryan
Ryan

Reputation: 281

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

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?

Upvotes: 16

Views: 8261

Answers (3)

Philip Oakley
Philip Oakley

Reputation: 14061

The commit 'number' is in fact its SHA1 hash value. This gives it great integrity for verification purposes.

It also has the nice side effect that you don't/can't expect it to be in any way sequential, which is also a natural property of branch & merge multi-user distributed VCS systems. There are lots of tree walk commands and methods of referencing commits, such as HEAD^ or HEAD^^.

git help revisons gives a full description of all the different syntaxes.

Upvotes: -2

holygeek
holygeek

Reputation: 16185

Doing git log or git show on the merge commit will show you the list of parents for the merge in the "Merge: " header. This lists the parents of the merge in sequential order from left to right. The first one is the first parent, the second one is the second parent, etc.

For example on the git repository:

$ git show 0af53e188a3a8915f65c3b3edaeed3e07d8d3802
commit 0af53e188a3a8915f65c3b3edaeed3e07d8d3802
Merge: b81b758 8894d53
Author: Junio C Hamano <[email protected]>
Date:   Thu Aug 11 11:04:28 2011 -0700

    Merge branch 'cb/partial-commit-relative-pathspec'

    * cb/partial-commit-relative-pathspec:
      commit: allow partial commits with relative paths

b81b758 is the first parent, and 8894d53 is the second parent for the merge commit 0af53e1.

The parent commits are numbered according to their order of appearance on the command line when the commit is done:

[on branch master]
$ git merge foo bar baz

That will merge the branches foo, bar and baz into master and create a new merge commit if fast-forward was not possible.

In the resulting merge commit "master" will be the first parent, "foo" 2nd, "bar" 3rd, and "baz" 4th.

Upvotes: 5

Amber
Amber

Reputation: 526573

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.

Upvotes: 17

Related Questions