Git Merge and Rebase

Git Merge vs Rebase

GIT is the most used and powerful among the Version Control Systems. We have already discussed Some Basic Git Operations in our Git Basics and Beginner Guide and In detail, we have discussed GIT Branch and its Operations. One of the other important operations in Git is Merge and Rebase. People often get confused with Git Merge and Rebase as both are performing similar operations. In this article, we will discuss what is Git Merge and Rebase and what is the difference between Git Merge vs Rebase.

Before getting into the concept, let’s assume the following scenario:

Git Branching
Git Branching

Let’s assume:

  • We have the Master branch with a commit history M1 and M2.
  • Then, we have a Developer D1 is working on a feature branch called Feature-1 and it has commits called F1-1.
  • Also, we have another Developer D2 who is working on another Feature called Feature-2 and it has commit called F2-2.
  • Now, we need to combine the work of two developers (F1-1 and F2-1) with the master branch.  

Both Merge and Rebase are doing the combining job, but there is some difference. First, we will discuss what is Merge and Rebase one by one.

Git Merge

In simple words, Got Merge is the process of integrating an independent line of feature branch taken from master to actual master

Git Merge
Git Merge

Branch Tip – is the latest commit or most recent commit of the branch.

As shown in the figure, we have the Master branch and its consecutive commits respectively M1 and M2. In this M2 is the latest of Master branch’s changes (Branch Tip). Also, we have Feature-1 branch from Developer-1 and we have the latest change as F1-1 commit (Branch Tip).

Now let’s run the command to Git merge

$ git checkout master
Switched to branch 'master’
$ git merge Feature-1
Merge made by the 'recursive' strategy.
README |    1 +
index.html |	4 +
2 file changed, 5 insertion(+)

Git Merge will take the latest changes in the two branches and integrate it as one branch. In our case, Git Merge will take the Master branch’s latest change as M2 commit (Branch Tip) and Feature branch’s latest change as F1-1 commit (Branch Tip) and merge together as M3. So, M3 will have changes of F1-1 of a feature branch and M2 of the Master branch.

Git Rebase

Rebase is the process of moving the commits to the top of another branch or branch tip.

Git Rebase
Git Rebase

The command to Git merge

$ git checkout Feature-2
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: added staged command

As mentioned in the above image, Git Rebase will take the changes of the master branch and move and append on top of the Feature-2 branch. Particularly, on Feature-2’s branch tip (F2-1). So, the Branch tip of Feature-2 branch will now have all the changes done after the Feature-2 branch’s cut down point.

Now let’s see the difference between Git Merge vs Rebase.

Git Merge vs Rebase

Conclusion

Git Merge and Rebase both are to integrate the different changes of different branches. But depends on the situation, we need to select which one should be done. In our upcoming article, we will discuss more Git operations. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOpsAgileDevSecOps and App Development.

Leave a Reply