Preface


Actually, this problem has been bothering me for a while, I believe that some people have the same trouble 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 much. Through my own rightgit Use, Plus asking colleagues for advice, I understand that, So write it down and share it, I'll try to elaborate

merge Andrebase Difference

Suppose we have a warehouse as shown in Figure 1, The warehouse hasmaster anddevelop Two branches, Anddevelop Is in(3.added merge.txt
file)commit Frommaster Pulled branches.


Figure 1


merge

Suppose nowHEAD stay(6.added hello.txt file) place, That is to saymaster Last branch submission, Execution at this timegit merge develop,
The results are shown in the figure below.


Figure two
How it works:git It is automatically based on 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 two.(7.Merge branch ‘develop’).
This ismerge Effect, In short, merge two branches and generate a new commit.


rebase

thatrebase That's how it works?
Assume that the initial state is also shown in Figure 1. Two branches and onemaster, Onedevelop, hereHEAD stay(6.added hello.txt
file) place, Now executegit rebase develop, The results are shown in Figure 3.


Figure three
Can seedevelop The branches are separated. The branches are gone, Let's explain how it works:
In executiongit rebase develop before,HEAD stay(6.added hello.txt file) place, When executedrebase Operation time,git
The common ancestor of two branches (3.added merge.txt file) Start extraction Current branch( At this timemaster branch) Last revision, Namely (6.added
hello.txt file) thiscommit, Again master Branch direction Latest commit of target branch( At this timedevelop branch) Namely(5.added
test.txt file)
place, Then apply the changes just extracted to the latest submission. If there are multiple changes extracted, thatgit Apply to the latest commit in turn, As shown in the following two figures, Figure 4 shows the initial state, Figure 5 is executionrebase Post state.

Figure four

Figure five
Simply speaking,git rebase The extraction is a bit likegit
cherry-pick equally, implementrebase Then submit the currentcherry-pick To target branch, The extractedcommit delete.


merge OR rebase

When will it be usedmerge, When to userebase What about?
Another example:
The initial state is shown in Figure 6:
As before,develop Branches are also in (3.added merge.txt
file) Frommaster Branch pulldevelop branch. What's different is that the two branches are differentcommit At different times, beforedevelop Branched4 and5commit staymaster branch3 after6 before, Now isdevelop Branched4 Submit earlier thanmaster Branched5 Submission,develop Branched6 Commit later thanmaster Of5 Submit earlier thanmaster Of7 Submission.


Figure six
In the case of the above figure, staymaster Branched7commit place, implementgit merge develop, The results are shown in Figure 7:

Figure seven
implementgit rebase develop, The results are shown in Figure 8:

Figure eight
1. It can be seen thatmerge The results reflect the timeline, howeverrebase Will disrupt the timeline.
2. andrebase Looks simple, howevermerge 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 projectsgit pull To pull the code,git pull Equivalent togit fetch + git merge, If running at this timegit
pull -r, that isgit pull –rebase, Amount togit fetch + git rebase

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

Acknowledgement: Thanks to my colleague Zhang Fangit Guidance on