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

3 outages within 1 month, is the 5G core public cloud reliable?

[[442675]] This article is reprinted from the WeC...

Some thoughts on the information construction of the financial industry

The rapid development of information technology h...

HostYun Los Angeles CN2 GIA line AMD series VPS simple test

We have shared information about VPS hosts in var...

Gcore: Miami E5-2623v4 CPU dedicated server 25% off, supports Alipay

The blog has shared G-core (gcorelabs) products a...

HTTP 2.0 Interview Pass: Mandatory Caching and Negotiated Caching

[[413787]] This article is reprinted from WeChat p...

5 blockchain trends for 2018

Few new technologies have generated as much discu...

Why can't the 5G package be changed back to 4G?

Recently, the "5G-forced" incident has ...

What is edge computing in IoT?

The growing number of “connected” devices is gene...

RAKsmart Los Angeles CU9929 line VPS simple test

Last month, the blog shared that RAKsmart Los Ang...