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

Popular posts from this blog

Stack vs. Queue

Stack is like a pile of  books placed on top of each other. We can add new books to the top and can remove them from only the top because stacks are LIFO which means last-in, first-out. Queues on the other hand are the opposite, which is FIFO meaning first-in, first-out. So adding an element to the queue will be the same but removing will happen to the first element not the last one. An example of a queue would be the wait line in front of any kind of service we see around us like the bus station or the shops,....etc.

Middlewares

Middlewares in Javascript are functions that come in the middle of the request-response cycle. They have access to both the request and the response object as well as the next middleware function to be executed; usually called next(). Popular examples on middleware include: body-parser, cors, session, cookie-seesion and cookie-parser.