50

In this documentation it is mentioned

A commit object may have any number of parents.

But from my understanding, the only case where a commit will have more than 1 parent is when a merge has happened, and in that case there will only be two parents. So my question is, can a commit have more than 2 parents? If so, when?

46

You can use git merge to merge more than one commit into your current branch. From man git-merge (or git help merge):

git-merge - Join two or more development histories together

The result will be a commit with more than two parents when you do that.

  • 34
    A merge of more than one branch (i.e. a commit with more than two parents) is colloquially known as an "octopus merge". That's also the source of inspiration for GitHub's logo and mascot, the eight-legged cat: it was originally called the Octopuss, but was renamed the more corporate-friendly Octocat. – Jörg W Mittag Mar 30 '16 at 3:05
  • 4
    What are the advantages of merging three or more branches in a single commit, instead of a series of commits? – Tor Klingberg Mar 30 '16 at 9:09
  • 2
    @TorKlingberg Cleaner history, but make sure to test the final product before pushing to the remote repsitory. – Ferrybig Mar 30 '16 at 9:23
  • 6
    @KasunSiyambalapitiya - There are multiple examples in one particular github repo. One of the many octopus merges in that repo which involves 27 parents. – David Hammen Apr 17 '17 at 13:58
  • 1
    @JörgWMittag Literally none of that is true. Source: worked at GitHub for five years. – gjtorikian Nov 13 '17 at 19:50
7

Yes, how about 100k parents?

Here is a live GitHub example with a merge of 100k commits: https://github.com/cirosantilli/test-octopus-100k Generated with this script.

Trivia

Linus does not like commits with more than 60 parents: https://www.destroyallsoftware.com/blog/2017/the-biggest-and-weirdest-commits-in-linux-kernel-git-history

It's pulled, and it's fine, but there's clearly a balance between "octopus merges are fine" and "Christ, that's not an octopus, that's a Cthulhu merge".

Have a look at the format for the Git commit object

https://stackoverflow.com/questions/22968856/what-is-the-file-format-of-a-git-commit-object/37438460#37438460

From that analysis, we can see that the list of parents list is an arbitrary newline separated list of type:

parent {parent_1_sha}
parent {parent_2_sha}
...
parent {parent_N_sha}

and so an arbitrary number of parents is allowed.

Minimal example

Script:

#!/usr/bin/env bash
set -eu

mkdir tmp
cd tmp
git init

touch root
git add .
git commit -m root
sha_root="$(git log -1 --format="%H")"

touch 1
git add .
git commit -m 1
sha1="$(git log -1 --format="%H")"

git reset --hard "$sha_root"
touch 2
git add .
git commit -m 2
sha2="$(git log -1 --format="%H")"

git reset --hard "$sha_root"
touch 3
git add .
git commit -m 3
sha3="$(git log -1 --format="%H")"

git merge -m merge "$sha1" "$sha2"

Output:

*-.   2d2a6c2 (HEAD -> master) merge
|\ \  
| | * 2300c18 2
| * | 7e096cb 1
| |/  
* | 50aa125 3
|/  
* a1e94fd root
  • I'm not sure if GitHub just can't handle a merge of that magnitude or if you've left it as a private repository. Or if GitHub is just having some unrelated issue. But regardless, the 100k merge link is a 500 error for me. – 8bittree Sep 4 '18 at 16:01
  • @8bittree it can't handle, private repo would be 400 :-) github.com/isaacs/github/issues/1344 – Ciro Santilli 新疆改造中心法轮功六四事件 Sep 4 '18 at 16:03
  • What is this connection between bearded sandal wearing FOSS advocates and Lovecraft that I repeatedly enounter? – Neutrino Sep 23 '19 at 15:29
3

You can specify more than one branch when merging.

For example:

git merge branch_A branch_B branch_C [...]

Then commit has more parents.

Not the answer you're looking for? Browse other questions tagged or ask your own question.