Git’s rebase function is assumed to be familiar with git, so we won’t cover other basic uses of git. Git is a tool, so the primary purpose of learning git is to understand what its functionality does. Here we will analyze rebase step by step.

Suppose we need to create a branch to develop new features for the product,

 git checkout -b feature

It can be seen that we currently have only two branches and have switched to the feature branch.

Next we will modify the test.txt file under the feature branch, add content_feature to it, save and commit.

git add test.txt

Git commit -m 'feature branch '

Take a look at the submission:

git log –graph –pretty=oneline –abbrev-commit 



At this point, the feature branch is complete, but sometimes we may also make changes to the same file under the master branch. For example, we add content_master to test. TXT under the master branch.

Again, save and commit.

git add test.txt

Git commit -m 'master branch '



Next comes the key point. When the development of the new feature is completed, it will be merged into the main branch.

git merge featture



You can see that there is a conflict in the merge content and we need to modify it manually. Change it to content_master_feature.



Next resubmit to complete the merge:

git add test.txt

git commit -m 'merge_master_feature'



You can see that the merge was successful.

. Finally, it’s time to rebase.

Rebasing is actually a way of merging branches. For example, we can switch to the feature branch and then use git reabse master command to merge branches. Why switch to the feature branch? And what does this command mean?

First of all, we need to know that when merge is used, it is actually a three-way merge, that is, the modified master branch and feature branch are merged based on the original master branch. Go figure:



It can be considered that C3 and C4 respectively represent the master branch and feature branch after modification, while C2 is the master branch before modification and serves as the basis of C3 and C4.

Rebase, rebase, rebase, rebase So who does that make the base branch? Stolen figure again:



It can be seen that c3 is taken as the basis, so it is time to modify the master branch, and the changes of the feature branch directly affect the master branch, forming a new master branch C4 ‘. Commands to do this:

git checkout feature

git rebase master



As before, the conflict needs to be resolved, and we are provided with the means to resolve it.

git add test.txt

git rebase --continue

At this point, the feature (C4 ‘) branch is changed based on the modified Master (C3) branch. You can then switch back to the Master branch and quickly merge the two branches.

$ git checkout master
$ git merge featureCopy the code



Speaking of which, it’s a bit of a curse, isn’t it, merge? Why bother to rebase? Please below:

Merging without base change:



4. A merger after rebasing:



Yeah, that’s right. The point of rebasing is to fix history, to merge branch history into the main line.

. Isn’t it, this function looks not nothing eggs, in fact, this function is needed in some scenarios will have obvious effect, such as when we maintain open source project to others commit changes, must first develop in their own branches, and then submit, but if we change the base and then submit maintenance staff wouldn’t have to integrate work, direct rapid consolidation.

I feel that these functions are still very far away for me,, so, first learn here, these more advanced functions you do not have the actual use of the scene, learn will soon forget, understand can understand, when really want to use, can also speed up our learning speed.

Reference:

Git tutorial

An in-depth explanation of some great god