Git Merge Vs Rebase

Diksha Pathak
3 min readOct 10, 2024

--

Source : Google

Git Rebase and Merge are both used to integrate changes from one branch to another but in a different way. Goal is same but the process of doing it differs.

Why do we need to integrate?

Say you freshly checked out a feature branch from master which is at commit K1 (master head) . You go ahead with writing your code and make 3 commits L1, L2, L3. Meanwhile your peer merges their commit to master branch and now the master head is at K2. And now, you are done with your changes and want to merge the commits but as soon as you try to do git push, your push gets rejected because your branch is not in sync with the upstream branch. This is where you are required to integrate the changes with whatever branch you are trying to push to.

Like we saw above integration can happen by two means — merge and rebase. Let’s see what are they and the difference between them.

Git Merge

As the name suggests, it merges the changes between two branches. It does so by making a merge commit on the feature branch which also preserves the commit history between two branches.

Source : phoenixNAP

Git Rebase

Rebasing is rewriting the base of the feature branch, there is one base when you checkout the feature branch, and when you wish to integrate you rewrite the base by pulling all new commits from master to your feature branch and then adding your commits on top of the rewritten base.

There is no additional commit involved, commit history is not preserved because it’s re-written like a linear history.

Source : phoenixNAP

Which is better?

  1. If you are a big team and wish to preserve all commit history as is, then git merge is a right choice because rebase resets the history.
  2. If multiple people are working on the same feature branch, then git merge is again a right choice, because rebase will confuse everyone as the commit history is reset.
  3. Rebase can be a good choice, if you’re a small team or only one person is working on a branch.
  4. Rebase also provides a linear git log history while merge provides a rather complex history.

While git merge is a safe option in most scenarios, it is solely left on the discretion of the development team with what they want to follow. Some teams prefer rebase over merge to avoid an extra commit and have a linear commit history while some prefer merge as it’s safe.

--

--