Interviewer: What is your understanding of Git Rebase and Git Merge? What are the differences?

Interviewer: What is your understanding of Git Rebase and Git Merge? What are the differences?

[[417941]]

This article is reprinted from the WeChat public account "JS Daily Question", the author is Huihui. Please contact the JS Daily Question public account to reprint this article.

1. What is

In a project that uses git for version management, when a feature is developed and merged into the master branch, there are two ways:

  • git merge
  • git rebase

git rebase and git merge have the same function, which is to merge the commits of one branch into another branch, but they are different in principle.

The usage of both is also very simple:

git merge

Merge the current branch into the specified branch. The command usage is as follows:

  1. git merge xxx

git rebase

Transplant the current branch to the specified branch or specified commit. The usage is as follows:

  1. git rebase -i < commit >

Common parameters include --continue, which is used to continue rebase after resolving conflicts.

  1. git rebase --continue  

2. Analysis

git merge

Merge the current branch with the xxx branch through git merge, and the resulting new commit object has two parent nodes

If the "specified branch" itself is a direct child node of the current branch, a snapshot merge will be generated

For example, the bugfix branch is forked from the master branch as shown below:

When merging the bugfix branch into the master branch, if the status of the master branch has not been changed, the history of the bugfix branch contains all the history of the master branch.

So by moving the location of the master branch to the latest branch of bugfix, the merge is completed.

If the history of the master branch has new commits after the bugfix branch is created, the following situation occurs:

When you use git merge at this time, a new commit will be generated, and the HEAD of the master branch will be moved to the new branch, as follows:

As can be seen above, the latest snapshots of the two branches and their most recent common ancestor will be merged in three ways. The result of the merge is to generate a new snapshot.

git rebase

Similarly, the history of the master branch has new commits after the bugfix branch was created, as follows:

Through git rebase, it will become as follows:

During the handover process, if a conflict occurs, you need to modify the respective conflicts as follows:

After rebase, the master's HEAD position remains unchanged. Therefore, to merge the master branch and the bugfix branch

As you can see from the above, rebase will find the most recent common ancestor of different branches, such as B in the figure above

Then compare the current branch with the previous commits of the ancestor, extract the corresponding changes and save them as temporary files (the old commits X and Y are not destroyed, they are simply no longer accessible or usable)

Then point the current branch to the target latest location D, and then apply the changes previously saved as temporary files in sequence

3. Difference

As you can see above, merge and rebasea are both merge history records, but they have different characteristics:

merge

Merging branches together will create a merge commit and link the history of the two branches together.

In fact, it is a non-destructive operation. The existing branch will not be changed in any way, but it will make the history record relatively complicated.

rebase

Rebasing moves an entire branch onto another branch, effectively integrating all commits from that branch.

The main benefit is that the historical record is clearer, and the differences are reflected on the basis of the original commits, eliminating the unnecessary merge commits required by git merge

References

https://zhuanlan.zhihu.com/p/361182707

https://yuweijun.github.io/git-zh/1-git-branching.html#_rebasing

https://backlog.com/git-tutorial/cn/stepup/stepup1_4.html

<<:  Google and Facebook to build new submarine cable connecting Japan and Southeast Asia in 2024

>>:  "5G+Industrial Internet" security capabilities and scenario-based solutions

Recommend

Is Your Ethernet Cable Faulty? Signs to Watch Out For

​Cables are an unwanted but necessary thing, and ...

What is Layer 4 and Layer 7 load balancing? What is the difference between them?

In the early days of the website, we generally us...

Amid the epidemic crisis, many countries are planning to break through with 5G

For the global 5G industry, the first quarter of ...

Link aggregation, redundancy, stacking, and hot backup of core switches

1. Link Aggregation Link aggregation is the combi...

What can 5G messaging bring to industry customers?

5G messaging is planned and built based on the GS...

Thoroughly understand Session and Token

Hello everyone, I am amazing. When building a use...

10,000-word article on DNS protocol!

[[376851]] Consider this question: how many ways ...