preface
Git merge syntax is used to merge branches. Git rebase can be used for branch merging, so let’s explore the differences between them.
First: The difference between the two
The main difference is in git logs: whether to keep the branch’s commit node.
Than: Detailed analysis
Premise:
- In one git project I created and saved two files: 1.txt and 2.txt labeled “1” and “2”;
- Create branch branch1;
- Continue to create and save two documents on the Master branch: 3.txt and 4.txt as’ 3 ‘and’ 4 ‘;
- Go to branch1 and create 5.txt and 6.txt as “5” and “6”;
At this point, we have documents 1, 2, 3, and 4 on the Master branch and documents 1, 2, 5, and 6 on the branch1 branch. Then we perform our merge operation.
-
Git merge:
Git merge git merge git merge
At this point, a new commit node “7” has been automatically created on the Master branch and the 5 and 6 commit of branch branch1 have been retained in git log.
Master 1, Master 2, Master 3, Master 4, branch1 5, Master 6 are recorded, and Master 7 is generated automatically:
-
Git rebase:
Git rebase on branch branch1
Branch1 merges all documents 1, 2, 3, 4, 5, and 6 from branch1.
Merge commit = merge commit = merge commit = merge commit = merge commit = merge commit = merge commit
-
Guess yourself:
Learned rebase, I guess immediately, if the merge in the branch baranch1 directly to merge the main branch, after get all the documents back to the main branch to perform the merge will also can get a line, it turns out, so want to explain is not fully understand the real meaning of rebase, look at the results…
SO (knock on the blackboard)
What exactly is Rebase?
Rebase is — rebase, rebase, rebase.
That is, change the base point of a branch so that the original branch continues from the specified node (commit).
In plain English, a rebase operation preserves the changes made by the commit, but deletes some of the existing commit records on the branch, removing these nodes.
Last: A comparison of the two
To compare | merge | rebase |
---|---|---|
advantages | Keep valuable historical documents | Cuts are numerous |
disadvantages | Branches are cluttered and redundant | It doesn’t reflect the timeline |
Therefore, considering the use of merge or Rebase or score, specific project analysis:
- Git Rebase is recommended for large projects that require a concise linear history tree for the leader to manage.
- Git Merge is recommended for small projects where you need to review history to facilitate writing process reports.