Which Option to Choose for Git Merge

When submitting a Merge Request (MR) for a project branch, there are three options: Merge, Rebase, and Squash.

What are the differences between these three options, and how should you choose? Here’s a summary.

https://static.1991421.cn/2024/2024-10-30-190528.jpeg

Merge

The essence of Merge is creating a new commit node, with all commits from the source branch brought over. If there were initially three commits, after the Merge, four commit nodes will exist, with one being the Merge Commit. The commit created by Merge also carries the source branch’s commit information. Therefore, in many Git GUI tools, these details form a commit relationship graph.

Rebase and merge

Rebase modifies the parent node of commits, moving all commits from the source branch to the end of the target branch. If there were three commits initially, after the Rebase, three new commit nodes will appear. Unlike Merge, it does not create a new commit node. Hence, in many Git GUI tools, it appears as a straight line.

Squash and merge

The essence of Squash is to merge the source branch’s commits into a single commit, generating a new commit message. In many Git GUI tools, this results in a single, latest commit node.

https://static.1991421.cn/2024/2024-10-30-220556.jpeg

Which to Choose?

Understanding the differences between the three, how should you decide? Here’s a summary:

  1. Prefer Squash because it reduces the number of commits, minimizes redundant commit messages, and makes the commit history clearer. A developer might make multiple commits on their branch. Choosing Merge or Rebase can clutter the main branch with too many commits, whereas Squash reduces the commit count. Of course, developers could also reset and re-commit in their branch, but that is more tedious.
  2. Rebase as a secondary option, though the downside is that it does not retain merge information. If you don’t care about the merging relationship between branches and only focus on committed content and authors, this can be a good choice.
  3. Lastly, Merge as it is the simplest and retains the merge relationship between branches. The downside is a more complex relationship graph.