Merge branches using Merge or Rebase?

As an ambitious developer, I would have preferred a better version management tool (Git), and it is inevitable that we will choose between Merge and Rebase for merging branches.

Both Rebase and Merge are designed to integrate changes you make from one branch to another, just in different ways. Although the purpose is the same, different methods have different advantages and disadvantages.

The difference between

For example, we have the following commits. Merge takes a combination of commits as a result, and rebase adds all of the commits to the target branch after the last commit.

As you can see from the figure above, merge has a history of merging, whereas rebase has no history and forms a straight line.

Merge

  • Easy to understand
  • The source branch and the target branch are separated
  • Preserve the commit history and branch graphs of functional branches
  • Once the branch is more displayed, it is more chaotic

Rebase

  • Simplify complex records and be linear readable
  • There is no record of merging
  • Multiple COMMIT conflicts must be committed one by one
  • Force push is required for the remote branch rebase

When to use Rebase? When to use Merge?

  • Independent development

    If you are not a team developer, then you may prefer to use Rebase to keep your commit history clean.

  • Ready to code review

    You need to submit a merge/pull Request to review your code, and then perform the merge. This will save a record of your merge request for future reference.

  • Merge into multiple target branches or someone else is using the current branch

    Merge should be used because when you perform rebase, the current branch’s original commit is deleted (affecting others) and new commit connections are formed after the target branch’s latest commit. So you can use rebase to merge branches if this condition is not true.

recommended

If the third point above is not met (merge to multiple target branches or someone else is using the current branch), personal branches (feature/bugfix/…) Use Rebase to update changes on the main branch (the source of the personal branch), make sure the current branch is up to date, and then submit the Merge /pull Request. Someone else is responsible for reviewing your code and deciding whether to approve the request, so that you can see the history of everyone developing the merge.

I don’t know how you are?

Review of previous articles

  1. Apply the “strategic pattern” to the actual project

  2. What have I learned from building a wheel

  3. Soft skills in technical interviews

  4. So what if you don’t override equals and hashCode at the same time!

Reply: interview video and architect send you very good information.