Git Rebase vs Merge
Tags: git, generalprogramming, versioncontroll
Published: 2019-05-07 06:48:30

---
title: Git Rebase vs Merge
published: true
description: In detail comparison between git rebase and git merge and when to use it
tags: Git, General Programming, Version controll
cover_image: https://thepracticaldev.s3.amazonaws.com/i/clskzj7qqdgbawjtjkcl.png
---

When you are using git, most probably you may used git merge or git rebase to combine two branches. Pretty much both of these commands do the same thing. But people select either of technique based on their requirements. This post is to compare the difference between git merge vs rebase and when to use.

Here below a diagram that visualize master branch to maintain the main code base and feature branch to develop some feature. You can see that feature branch was create from the master branch. While changes happening in feature branch, master branch also changing with new commits.
![branch](https://thepracticaldev.s3.amazonaws.com/i/nqo6mgud5dzbmfpxzqkd.png)

Once you completed the feature implementation on feature branch, you need to combine it with the latest master branch. Here, you have two options. Either you can merge or rebase feature branch with the master.

## Git Merge
If you merge two branches, it will create a new commit and combine master branch into the feature branch. Here, feature branch commits keep as it is and commit history does not change. We can represent above digram for git merge command as follows.

![git merge](https://thepracticaldev.s3.amazonaws.com/i/ikh61zarn8kmz37qgmmx.png)

You can merge master branch into the feature branch by running following command
```
git checkout feature
git merge master
```
or with single line
```
git merge feature master
```
## Git Rebase
Rebase is little different than merge. Rebase apply all feature branch changes on top of master branch by creating new commit for each of its previous commit messages. Which means that rebase command will change your commit history and regenerate commits again on top of master branch. Final output of git rebase can be represent as follows.

![Rebase](https://thepracticaldev.s3.amazonaws.com/i/3i914xy87kdqzcevc0c9.png)

Following command can be used to rebase feature branch on top of master branch. "-i" option used for interactive rebase. Otherwise you can simply use "git rebase master".

```
git checkout feature
git rebase -i master
```

## Rebase or Merge
You can use either rebase or merge to combine two branches. Peoples are tend to use rebase over merge due to following facts.

- There is no additional commit added to the merged branches
- Rebase commit history is cleaner than merge history since rebase history does not have complex branches
- Due to the branch complexity in merged git tree, some code changes are invisible. But rebase changes come from a specific and entitled commit.
- Rebase make easier to use git log, git bisect, and gitk commands.

## When to not to use Rebase

As we already discussed, rebase change the commit history. Therefore, it should not be apply on the public branches(eg: master) where other people are working on. This make git confuse that your master branch diverge from others master branch.

## Conclusion

Both merge and rebase can be used to combine two branches. Merge command just unify your work with a commit without changing history. While rebase apply feature branch changes on top of master branch and change the history. If you prefer to have clean history, then you can use rebase. If you need to preserve the history changes, then merge would be the best choice.

Hope, this article helps you to learn the difference of git merge and git rebase. Comment down what is your preference and the reason. See you with another article. Cheers :).

## References
[https://www.atlassian.com/git/tutorials/merging-vs-rebasing](https://www.atlassian.com/git/tutorials/merging-vs-rebasing)