Git Merge vs Rebase

Yuvraj Kale
3 min readMay 16, 2020

Many developers use Git to store their code at a single place and use it from anywhere for doing this there are many steps but not every developer understood why that step needs to be taken. Here I am trying to clear the basic difference between Merge and Rebase.

Photo by Pankaj Patel on Unsplash

Merge

Merge is nothing but combining two files into one single file.

But how this is done in Git and many developers don’t understand it.
But in the git world, merging is a way of combining the code of feature branch(i.e new branch which is derived from its super branch) into parent branch(i.e super branch from which feature branch is derived).

Merge

Example 1:

Let’s take an example, We have a “master” branch where the final code is present. Now the client has asked for a few changes in our project. That time we have cloned master in our machine but we can’t start changes directly into the master branch as it’s final code of the fine working project. So we created a new branch “feature1” and start working on that. Now we have all changes done and now we want that “feature1” code should be available to master then we try to combine that “feature1” code to “master” with the command or by using the browser. This process of combining the code of the derived branch to the super branch is called a Merge.

Rebase

Rebase is also a process of combining the code of two branches. The only difference between Merge and rebase is

Merge combine changes of subbranch(i.e derived branch) to super-branch

and

Rebase combine changes of super-branch into sub-branch(i.e derived branch)

Now you have a question that how could it’s possible that there are any changes in the super-branch. Yes, it’s possible this condition occurs when multiple developers working on the same project and created multiple branches from the super-branch. Let’s see in the following example.

Rebase

Like in Example 1,
We have a master branch from which the “feature1” branch is derived and another developer is also working on same code and he created the “feature2” branch and he is working on it. Now “feature1” code is merged to master and “feature2” wants to use the updated code of “master” that time master code is combined into the “feature2” branch this process is called rebase.

Rules to rebase your code
1)
Commit your local changes to the origin i.e feature2”.
2) Checkout to super-branch in above example checkout to “master”
3) Pull all the changes of super-branch i.e “master”.
4) Now checkout to sub-branch i.e “feature2”
5) Now rebase “feature2” with “master”

I have not given any command to perform above action.

Conclusion

This is how Rebasing and merging are designed to integrate the changes of one branch into another branch in different ways. Only we have to keep mind both concepts help to combine code.

--

--