Skip to main content

Git rebase vs Git merge

While working with git and github one will always face the need to combine their changes with the master branch of the team repository. to do this we have two solutions; we either use git merge or git rebase. To explain the differences between these two options we are going to consider the following example, you start working on feature branch for the sidebar of your website and other team members are updating the master branch with new commits. This will result in forked history as shown in the picture below.

A forked commit history

Merge:

We can do the easiest option which is merging the master branch into the sidebar branch with this git command:

git merge sidebar master

This will create a new commit for the merge that connects both of these two branches, giving what looks in the picture below.

Merging master into the feature branch
So as you have seen merging is easy and really simple to work with. It's also a non-destructive operation; by this I mean that the already existing branches are not changes in any way. 

Rebase:

The other option would be rebasing the sidebar branch on top of the master branch using these commands:

git checkout sidebar
gi rebase master

This will move the entirety of the sidebar branch on the tip of the master branch, doing that will incorporate all of the commits in master. But this will re-write all of the commit history because git rebase is a destructive operation.

Comments