preface


In fact, this problem bothered me for a while , I believe that some people have the same problem as me , There are many explanations on the Internet , But either there's no picture , Or it's a mess of explanations , I don't understand , Through my own right git Use of , Plus asking colleagues for advice , I understand that , So write it down and share it , I'll try to elaborate

merge And rebase The difference between

Suppose we have a warehouse as shown in Figure 1 , The warehouse has master and develop Two branches , And develop Yes (3.added merge.txt
file)commit From master Pulled branches .


Figure 1


merge

Suppose now HEAD stay (6.added hello.txt file) place , That is to say master Last branch submission , Execute at this time git merge develop,
The results are shown in the figure below .


Figure 2
How it works :git It will automatically follow the common ancestor of the two branches (3.added merge.txt file) this commit And the latest commits of both branches are
(6.added hello.txt file) and (5.added test.txt file) Conduct a tripartite merger , Then generate a new
commit, That is, figure 2 (7.Merge branch ‘develop’).
This is merge Effect of , In short, merge two branches and generate a new commit .


rebase

that rebase That's how it works ?
Assume that the initial state is also shown in Figure 1 . Two branches and one master, One develop, here HEAD stay (6.added hello.txt
file) place , Execute now git rebase develop, The results are shown in Figure 3 .


Figure 3
Visible develop The branches are separated. The branches are gone , Let's explain how it works :
In execution git rebase develop before ,HEAD stay (6.added hello.txt file) place , When executed rebase During operation ,git
The common ancestor of two branches (3.added merge.txt file) Start extraction Current branch ( This is master branch ) Changes on , Namely (6.added
hello.txt file) this commit, Again master Branch direction Latest commit of target branch ( This is develop branch ) Namely (5.added
test.txt file)
place , Then apply the changes just extracted to the latest submission . If there are multiple changes extracted , that git Apply to the latest commit in turn , As shown in the following two figures , Figure four shows the initial state , Figure 5 is execution rebase Status after .

Figure 4

Figure 5
In a nutshell ,git rebase The extraction is a bit like git
cherry-pick equally , implement rebase Then submit the current cherry-pick To target branch , The extracted commit delete .


merge OR rebase

When will it be used merge, When to use rebase What about ?
Another example :
The initial state is shown in Figure 6 :
As before ,develop Branches are also in (3.added merge.txt
file) From master Branch pulling develop branch . What's different is that the two branches are different commit At different times , before develop Branching 4 and 5commit stay master branch 3 after 6 before , Now it's develop Branching 4 Submit before master Branching 5 Submit ,develop Branching 6 Submitted after master Of 5 Submit before master Of 7 Submit .


Figure 6
In the case of the above figure , stay master Branching 7commit place , implement git merge develop, The results are shown in Figure 7 :

Figure 7
implement git rebase develop, The results are shown in Figure 8 :

Figure 8
1. It can be seen that merge The results reflect the timeline , however rebase Will disrupt the timeline .
2. and rebase Looks simple , however merge It doesn't look very simple .
3. The end result is that it's all combined , So how to use these two commands to see the needs of the project .


One more thing is , Frequently used in projects git pull To pull the code ,git pull Quite so git fetch + git merge, If running at this time git
pull -r, that is git pull –rebase, amount to git fetch + git rebase

Finally, some suggestions git Visualization tools <https://git-scm.com/download/gui/mac>
, I use gitkraken, The functions of these tools are basically the same , It's easy to use

Acknowledgment : Thanks to my colleague Zhang Fan git Guidance on