gitmergegit-mergerebasegit-rebase

What's the difference between 'git merge' and 'git rebase'?


What's the difference between git merge and git rebase?


Solution

  • Suppose originally there were three commits, A,B,C:

    A-B-C

    Then developer Dan created commit D, and developer Ed created commit E:

    A-B-C-D-E

    Obviously, this conflict should be resolved somehow. For this, there are two ways:

    MERGE:

    A-B-C-D-E-M

    Both commits D and E are still here, but we create a merge commit M that inherits changes from both D and E. However, this creates a diamond shape, which many people find very confusing.

    REBASE:

    A-B-C-D-E-R

    We create commit R, whose actual file content is identical to that of merge commit M above. But, we get rid of commit E, like it never existed (denoted by dots forming a vanishing line). Because of this obliteration, E should be local to developer Ed and should have never been pushed to any other repository. The advantage of rebasing is that the diamond shape is avoided, and history stays a nice straight line - most developers love that!