“My math always good, last time I like the girl ask me a topic: you are not a number good, ask you a topic, 1+9+0=? . Such a simple question can rarely pour me, I casually answer: 10. Then she ran away crying, saying: serve you right you don’t have a girlfriend.”
These days the circle of friends have been long living creature this monster to occupy, stupid uncle hair drip no what people see, above that paragraph, we can leave a message, that girl want to express what meaning?
Everyone may know git this creature, he is created by the father of Linux, just ten years time has occupied the first place in the code version management software, even the arrogant Microsoft secretly began to use Git, you say that the development speed of Git is not the same as the cell reproduction.
Git is known for its natural love of branches, so an important aspect of branch management is branch merging. Git rebase git rebase git rebase git rebase git rebase
What’s the difference?
Let’s assume that a Git repository has a master branch and another dev branch. As shown in the figure below.
The ABCDEFG nodes (each of which is a COMMIT) are committed in chronological order, as shown in the following table.
node | Submit time |
---|---|
A | On July 1 |
B | On July 2 |
C | On July 3 |
D | On July 4 |
E | On July 5th |
F | On July 6 |
G | July 7 |
That is:
- The master branch has four nodes: A->B->C->E
- The dev branch has 5 nodes: A->B->D->F>G
Each node represents a COMMIT.
If the following two commands are executed:
$ git merge master
$ git rebase master
Both commands merge the master branch into the current branch, so what’s the difference?
Let’s start with the merge command.
After git merge master, the dev branch becomes the following figure.
We can see that after the gitmerge master command is executed, commits on the dev branch are merged based on the timeline.
What happens if you run git rebase master?
Git rebase the git rebase command is used to change a string of commits based on which branch. For example, git rebase master is used to base the D, F, and G commits of the dev branch on the latest master branch. A common use of Git rebase is to keep the branch you are developing (such as dev) up to date relative to another branch (such as Master).
02 It’s better to experiment
Well, that said, let’s do the experiment.
Let’s say we create a local Git and create the following nodes on the master branch.
There are four ABCE nodes on the master branch.
Next we create the five ABDFG nodes on the dev branch, as shown in the figure below.
Now that we have the master and dev branches set up, we are ready to experiment. Let’s try git Rebase Master first.
$ git checkout dev
$ git rebase master
With that done, let’s take a look at the dev branch.
Is the execution result as we expected? The rebase command will cause the current DFG node to grow from the last node on the master node, e. the result of this command is A->B>C->E->D->F->G
Git merge master
$git checkout dev
$git merge master
The final result is as follows:
It’s the same thing as our prefetch. The result of the merge is as follows:
– A – > B > C > D – > E – > F – > G – > the merge node
Let’s sum up
Both merge and rebase commands are used to merge branches. When to merge and rebase?
If you need to merge other people’s changes, consider using the merge command. For example, if you need to merge other developers’ branches in project management, you can use the merge command.
ø When your development work or patch submission needs to be based on a branch, use the rebase command, such as submitting patches to the Linux kernel community.