13

When a commit has multiple parents, like this one, we see that it has 4 additions and 4 deletions. My question is compared to what? Are the additions and deletions compared to the file as it existed in BOTH parents? Or how exactly is it compared?

Shamoon
  • 41,293
  • 91
  • 306
  • 570

1 Answers1

13

It is a three-way merge between:

(you can see another example of three-way merge in this answer)

By convention, GitHub will always display the parents as:

  • The first parent, which is the branch you were on when you merged,
  • and the second parent, which is the commit on the branch that you merged in.

Compared to the common ancestor of those two commits, the second one, when merged into the first one, brings 4 additions and 4 deletions.


The phrase "Compared to the common ancestor of those two commits" is important for understanding the context in which the changes are being made.

When a merge commit happens in Git, the changes are calculated relative to the common ancestor of the two branches being merged. This common ancestor is the most recent commit that the two branches share.

Let's take a simplified example:

  1. You have a branch A where the latest commit is A1.
  2. You create a new branch B from A at commit A1. You make changes and commit them as B1.
  3. Meanwhile, A also has changes committed as A2.

Now, you want to merge B into A. The common ancestor here is A1, the point where B branched off. The changes brought in by B (in commit B1) are compared to this common ancestor (A1), not the latest commit on A (A2).

In this case, the merge will be a three-way merge between A1 (common ancestor), A2 (latest commit on A), and B1 (latest commit on B). Git will try to apply the changes from A1 to B1 onto A2. The commit stats (additions and deletions) are calculated relative to this common ancestor (A1).

The phrase "Compared to the common ancestor of those two commits" emphasizes that the changes are not directly compared between the two branches at their latest commits, but rather from the point they diverged.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • is there any way of identifying whether each commit is a merge commit or are there are mulitple parents to that commit in github API level – Kasun Siyambalapitiya Apr 17 '17 at 12:00
  • From `man git-merge` it says that `git-merge - Join two or more development histories together` which may lead to multiple parent commits ( more than 2) for a certain commit. in that case how the stats are calculated – Kasun Siyambalapitiya Apr 17 '17 at 12:33
  • can you please have a look on this question http://stackoverflow.com/questions/43452249/how-to-indentify-which-commit-introduce-which-changes-in-a-octopuss-merge/43453033#43453033 – Kasun Siyambalapitiya Apr 18 '17 at 03:12
  • 1
    @KasunSiyambalapitiya I have, and agree with torek's comment. – VonC Apr 18 '17 at 04:26
  • @VonC, why add the words "*Compared to the common ancestor of those two commits*"? – Pacerier Jun 04 '23 at 23:12
  • @Pacerier I have edited the answer to address your comment. – VonC Jun 05 '23 at 05:28