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

These five points cannot be ignored when selecting enterprise SD-WAN!

As the main theme of today's IT industry, clo...

Will 5G replace NB-IoT immediately after commercialization? Not really!

From May 21 to May 25, the international telecomm...

2021 Bots Automation Threat Report: An In-depth Analysis of Bots Attacks

Recently, as a professional manufacturer in the f...

3 Reasons Your IoT Needs SD-WAN

We live in an era of fast-paced digital transform...

VXLAN L3 applies EVPN to present a complete overlay network

Preface VXLAN (Virtual eXtensible LAN) is an over...

HostNamaste: $18/year-1.5GB/30GB/1.5TB Los Angeles & Dallas data centers

HostNamaste is a foreign hosting company founded ...

5G and AI Use Cases - How 5G Helps Implement Artificial Intelligence

Michael Baxter says 5G will unlock the potential ...

How 5G will benefit the Internet of Things

In this article, we want to turn our attention to...