Understanding Rebase (And Merge) in Git
Tags: git, github, webdev
Published: 2020-10-20 15:26:53

While merging is definitely the easiest and most common way to integrate changes in Git, it's not the only one: "Rebase" is an alternative (and slightly advanced) means of integration.

<hr>

## The Case Against Merge Commits

A normal commit, carefully created by a human being, is a meaningful unit: it wraps only related changes and annotates them with a comment.

All this gets lost in a **merge commit**: it's created automatically by Git to cram in all the differing changes. It has no semantic meaning, no separation of topics. Of course, the contents of those individual commits are preserved. But the history - commented and nicely separated into individual, meaningful commits - that lead to this point is not preserved in a merge commit.

This is why some people dislike merging and instead use rebase.

<hr>

## The Beauty of Rebase

Instead of cramming all changes into a merge commit, a rebase preserves the original commits. The project's history then looks as if it had evolved in a single, straight line. No indication remains that it had been split into multiple branches at some point.

![end-situation-for-rebase](https://dev-to-uploads.s3.amazonaws.com/i/wzasove9v5z5r0pn0reo.png)
 
Let's walk through a rebase operation step by step. The scenario is simple: we want to integrate the changes from branch-B into branch-A, using rebase.

![starting-situation-before-rebase](https://dev-to-uploads.s3.amazonaws.com/i/9d8262uevu4v0o2xjbk5.png)

The command for this is very easy:

```
$ git rebase branch-B
```

<hr> 

First, Git will "undo" all commits on branch-A that happened after the lines began to branch out (after the common ancestor commit). Of course, it won't discard them but rather save them away, temporarily.

![rebase-step-1](https://dev-to-uploads.s3.amazonaws.com/i/dod6ppzw8ujwvp50ctet.png)
 
Next, it applies the commits from branch-B that we want to integrate. At this point, both branches look exactly the same.

![rebase-step-2](https://dev-to-uploads.s3.amazonaws.com/i/d1abhyof3ctlehgjjc94.png)

In the final step, the new commits on branch-A are now reapplied - but on a new position, on top of the integrated commits from branch-B (they are _re-based_).

The result looks like development had happened in a straight line. Instead of a merge commit that contains all the combined changes, the original commit structure was preserved.

![rebase-step-3](https://dev-to-uploads.s3.amazonaws.com/i/q2pl44eewqqzigpg1acu.png)

<hr>

In case you're using the [Tower Git GUI](https://www.git-tower.com/?utm_source=devto&utm_medium=guestpost&utm_campaign=understanding-rebase-merge-in-git), you can use rebase simply by dragging the to-be-integrated branch and dropping it on the target branch, with the ALT key pressed. That's it!

![rebase-in-tower](https://dev-to-uploads.s3.amazonaws.com/i/scsj5ziezfos1nnivk2j.gif)
 
<hr>

## Learn More

Of course, there's a lot more to know and understand about rebase in Git! If you want to learn more, have a look at the chapter "[Rebase as an Alternative to Merge](http://git-tower.com/learn/git/ebook/en/desktop-gui/advanced-topics/rebase/?utm_source=devto&utm_medium=guestpost&utm_campaign=understanding-rebase-merge-in-git)" in my **free** online book on "_Learning Version Control with Git_".

[![ebook-learn-version-control](https://dev-to-uploads.s3.amazonaws.com/i/doqwrx4awh5pv48pc9ac.png)](http://git-tower.com/learn/git/ebook/en/desktop-gui/advanced-topics/rebase/?utm_source=devto&utm_medium=guestpost&utm_campaign=understanding-rebase-merge-in-git)