This is the fourth day of my participation in the November Gwen Challenge. See details: The last Gwen Challenge 2021.

preface

Git rebase merges code from one branch to another. Git rebase merges code from one branch to another. There are many articles on the Internet to explain the difference, but most of what is said is very vague and not clear enough, and what you read is also a little understanding. If an article is not well understood for readers, it is irresponsible for readers. This article will use the most familiar and easy to understand language to make you understand the difference thoroughly.

First the conclusion:

  • Merge is a merge operation that merges the changes made in two branches. By default, the merge operation commits the changes made in the merge
  • Merge’s commit history records what actually happened, focusing on the actual commit history
  • Rebase does not merge, but simply extracts the changes of the current branch and copies them after the latest commit of the target branch
  • The rebase operation discards any commit that has been made by the current branch, so do not perform the rebase operation on a branch that has been pushed to a remote location and is being developed in collaboration with others
  • Merge and Rebase are both good branch merge commands. They are neither good nor bad, and the choice of which to use depends on your team’s actual development needs and scenarios

Git Merge

As shown in the figure above, we have two branches, the Master branch and the Test branch. The test branch is created based on the submission node of the Master branch at B. After the creation, the Master branch is iteratively submitted twice. We’re at the F node. They bifurcate from B.

If we want to merge the test branch into the master branch, how does the merge work?

Git merge test git merge test git merge testCopy the code

As you can see from the figure, a new commit G is generated. How is this generated? The merge command merges the latest snapshots of the two branches (F, E, and D, C) and their most recent common ancestor (B), resulting in a new snapshot G (and commit).

Git rebase

Git rebase test git rebase test git rebase testCopy the code

This is different from the merge command

There’s a noun definition that we’re going to talk about very briefly

  • Test: base branch, target branch
  • Master: indicates the current branch

When a rebase operation is performed, Git extracts the changes on the base branch from the common ancestor of both branches, points the base branch to the latest commit, and applies the extracted changes to the last commit of the base branch.

This sentence may sound around, don’t be afraid, listen to the egg to give you a good translation:

Git rebase test: git rebase test: git rebase test: Git rebase test: Git rebase test: Git rebase test: Git rebase test: Git rebase test Git will save the C and D commit, then point the master branch to the test branch’s latest commit node (F), and add the C and D commit to F. Generate a new C’,D’, although the contents of C’,D’ are the same as the original C,Dcoommit, but the commit ID is different.

The rebase operation can be explained in one sentence by changing the base. The original base for the master branch was A, but now the new base is F, the latest commit from the test branch.

conclusion

Which operation is better, merge or Rebase, depends on different scenarios.

Git pull-r or git pull –rebase is recommended when pulling the latest code from the common branch, but the drawback is that after rebase I don’t know which branch my current branch was originally pulled from, because the base has changed. (If merge is used, a meaningless commit record is added).

When merging code on a common branch, use merge. For example, the main branch is the master branch. I have an egg branch, and I write a lot of junk code on the egg branch. If the master branch is rebase, its commit history will be deleted. If the master branch is rebase, its commit history will be deleted. If the master branch is rebase, its commit history will be deleted. My colleagues will probably crush my eggs, so don’t do that.