Ajedi32
Ajedi32

Reputation: 48368

How are git parent numbers assigned?

From the documentation for git-rev-parse:

^, e.g. HEAD^, v1.5.1^0

A suffix ^ to a revision parameter means the first parent of that
commit object. ^<n> means the <n>th parent (i.e. <rev>^ is equivalent
to <rev>^1). As a special rule, <rev>^0 means the commit itself and is
used when <rev> is the object name of a tag object that refers to a
commit object.

I understand that git commits can have multiple parents, and that this syntax can be used to disambiguate which parent is referred to, but what determines which parent is the 'first parent' or 'second parent'? Is it just based on which commit was checked out when the commits were merged?

For example, would git checkout master; git merge feature result in master being parent 1, whereas git checkout feature; git merge master would result in feature being parent 1? Or is there something else going on here?

Upvotes: 3

Views: 3009

Answers (2)

VonC
VonC

Reputation: 1324148

Note there is a limit to the number of parents a commit can have.

Git 2.24 (Q4 2019) illustrates that by fixing a bug related to said limit.

See commit 59fa5f5, commit a678df1 (15 Sep 2019) by René Scharfe (rscharfe).
(Merged by Junio C Hamano -- gitster -- in commit 773521d, 07 Oct 2019)

sha1-name: check for overflow of N in "foo^N" and "foo~N"

Reject values that don't fit into an int, as get_parent() and get_nth_ancestor() cannot handle them.
That's better than potentially returning a random object.

If this restriction turns out to be too tight then we can switch to a wider data type, but we'd still have to check for overflow.

That is because of:

rev-parse: demonstrate overflow of N for "foo^N" and "foo~N"

If the number gets too high for an int, weird things may happen, as signed overflows are undefined.
Add a test to show this; rev-parse "successfully" interprets 100000000000000000000000000000000 to be the same as 0, at least on x64 with GCC 9.2.1 and Clang 8.0.1, which is obviously bogus.

So:

what determines which parent is the 'first parent' or 'second parent'?

If you enter a too important number... you could end up with any parent.
Unless you are using Git 2.24 or more.

Upvotes: 0

user3426575
user3426575

Reputation: 1773

Is it just based on which commit was checked out when the commits were merged?

That's right. You can look at the parents of a commit using git show or git cat-file -p HEAD.

Upvotes: 4

Related Questions