Git Guides‎ > ‎

Merge vs Rebase

On the surface, the merge and rebase operations in Git appear to serve the same purpose: Take the changes from one branch and move them onto another.  And when applied successfully, they should yield the same code in the end.  So why would anyone perform a rebase when they could just keep on doing merges?

To answer that question, we need to know what happens with each operation.

What Happens When You Merge

The merge operation finds all the commits on the specified branch that are not on the current branch and applies it on top of the current branch.

This essentially is a completely additive operation.  You'll see an automatically created commit that records the merge.

What Happens When You Rebase

The rebase operation takes the current branch, rewinds the updates you've made on it, moves the starting point to the head of the specified branch and reapplies your changes on top of the new starting point.

This is a bit more complicated than just a merge.  And unlike the merge, you won't see a commit for the rebase unless you have to manually resolve conflicts.

Why Rebase

The rebase offers a better guarantee that you've incorporated the changes that took place from the original starting point of the branch to the newly specified starting point.  It really is as if you've started your work from that point.

In the context of team development, with multiple contributors and integration branches, being able to just move the starting point simplifies the issue of whether or not you've incorporated the latest updates into your development.  You don't have to follow a trail of commits to see what's made it into your development branch or go through an detailed analysis of commit diffs to ensure that you resolved manual conflicts in the same way that everyone else did.

Why Merge

Okay, so now that it seems that rebases are really wonderful, why would anyone merge?

A first scenario is simply from the organization hierarchy.  An integration branch should be thought of as the parent of a development branch.  Updates made on the development branch should merged onto the integration branch.  You're adding commits from a child back up to a parent.

A second scenario is that you're adding updates from parallel development efforts.  For instance, you're combining features that multiple teams have worked on in order to put together a release.

Merges are really how information is combined and pushed upstream in the repository organization.

A rebase is how those changes should flow back downstream from the parent to a child branch.
Comments