Christian Christian -3 years ago 123
Git Question

Are git branches squashed or replayed when merging?

Given a repo with a master and a feature branch and commits

as below:

C0 <-- C1 <-- C2 <------ ??? master
\ /
C3 <-- C4 <-- C5 feature

When I merge, what happens at the

Do I get one commit
, i.e. a "squash" of the merge as a single commit:

C0 <-- C1 <-- C2 <------ C6 master
\ /
C3 <-- C4 <-- C5 feature

Or a "replay", i.e.
C6 <-- C3' <-- C4' <-- C5'
(with the
because these commits now also contain

C0 <-- C1 <-- C2 <------ C6 <-- C3' <-- C4' <-- C5' master
\ /
C3 <-- C4 <-- C5 feature

Is it the same on GitHub when merging a box-standard pull request from the same repo's branch?

Answer Source

What you get is neither a squash nor a replay of the commits.

To get a squash you could use the --squash option to merge. Then you'd get

C0 <-- C1 <-- C2 <-- C3C4C5   master
        C3 <-- C4 <-- C5      feature

where C3C4C5 is a single commit with one parent whose diff from C2 is equal to C5s diff from C1 (barring conflicts).

The replay the changes from the branch, you could perform a rebasing operation. This might yield

C0 <-- C1 <-- C2 <-- C3' <-- C4' <-- C5'   master    featurE_ref
        C3 <-- C4 <-- C5      feature_ref_used_to_be_here

(or some variation, depending exactly what you do). In this case commit C3' differs form C2 exactly as C3 differs form C1, etc.

But in the case of a merge you don't get either of those. You get

C0 <-- C1 <-- C2 <------ M     master
       \                /
        C3 <-- C4 <-- C5      feature

'M' is a single commit, but unlike a C3C4C5 from the squash, M has two parents. Its TREE (content) differs from C2 in the same way that C5s TREE differs form C1, so it's similar to a squash. But it generally is interpreted as not introducing changes (except in the case of merge conflicts or evil merges); rather it marks that the changes from C5 and its ancestors are introduced into master at this point. Hence by default git log doesn't display a patch for M but does include commits C3, C4, and C5

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download