There is always a question in everyone’s mind what to choose between merge and rebase!!
This blog might help you to get such answers
- The first thing to understand about git rebase is that it solves the same problem as git merge.
- Both of these commands are designed to integrate changes from one branch to another branch– they just do it in very different ways.
Consider what happens when you start working on a new feature in a dedicated branch, then another team member updates the master branch with new commits, This results in history.
Now, let’s say that the new commits in a master are relevant to the feature that you’re working on. To incorporate the new commits into your feature branch, you have two options: merging or rebasing.
The Merge Option
The easiest option is to merge the master branch into the feature branch–
$git checkout feature $git merge master Or you can combine above commands $git merge feature master
This creates a new “merge commit” in the feature branch that ties together the histories of both branches.
The Rebase Option
As an alternative to merging, you can rebase the feature branch onto the master branch.
$git checkout feature
$git rebase master
- This moves the entire feature branch to begin on the tip of the master branch, effectively incorporating all of the new commits in master. But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch.
- The major benefit of rebasing is that you get a much cleaner project history. First, it eliminates the unnecessary merge commits required by git merge.
- Second, rebasing results in a linear project history.
Let’s take an example to get a better understanding
Assume we have three branches master, develop and feature branch.
All three branches in starting have a single commit right now
We can check the status of commits using ” $ git log –oneline ” by checking out in each branch.
Now, we will add one more commit on the master branch, by adding a text file by the name main_branch.txt
Add one more commit on develop, by adding a text file by name develop_branch.txt
Merge develop with the main branch, and you can see the number of commits in each branch afterward, there would be an
extra commit on develop branch.
Add one more commit on develop, by adding a text file by name feature_branch.txt