The difference between Git rebase, merge and stash

stash

It means to temporarily store the files that you have modified to the local, and restore your branch to an unmodified state. You can store infinitely. The order of storage is reversed, and the last submission is sorted at the top. Use list to view the stored list.

stash related commands

Store current changes:

git stash save "message" 

or

git stash "message"

View storage list:

git stash list

Restore the stored changes:

git stash apply

Restore the specified storage modification:

git stash apply stash@{
    
    l}   #l 是指当前存储列表中的顺序 0,1,2,3...

Clear the storage list:

git stash clear

Remove the specified storage modification:

git stash drop stash@{
    
    l}  #  l 是指当前存储列表中的顺序 0,1,2,3...

The basic operation of rebase

The development task diverged into two different branches, and each submitted an update.
Insert picture description hereThe easiest way to integrate branches is the merge command. It will merge the latest snapshots of the two branches (C3 and C4) and the nearest common ancestor (C2) of the two branches in a three-way merge. The result of the merge is to generate a new snapshot (and submit).
Insert picture description hereIn fact, there is another way: you can extract the patches and modifications introduced in C4, and then apply them once on the basis of C3. In Git, this operation is called rebasing. You can use the rebase command to move all the changes submitted to one branch to another branch, just like "replaying".

Insert picture description hereIts principle is to first find the nearest common ancestor C2 of these two branches (that is, the current branch experiment, the target base branch master of the rebase operation), and then compare the previous commits of the current branch with respect to the ancestor, extract the corresponding modifications and save them as temporary File, then point the current branch to the target base C3, and finally apply the changes previously saved as a temporary file in order. (Annotation: The commit id is stated for easy understanding, the same below)

At this time, the snapshot pointed to by C4' is exactly the same as the snapshot pointed to by C5 in the example using the merge command above. There is no difference in the final result of the two integration methods, but the rebasing makes the submission history cleaner. When you look at the history of a branch that has undergone rebase, you will find that although the actual development work is parallel, they look like serial, and the commit history is a straight line without bifurcation.

rebase and merge

Whether through rebase or merge, the final result of the integration will always point to the same snapshot, but the commit history is different.

rebase是将一系列提交按照原有次序依次应用到另一分支上
merge是把最终结果合在一起。

Rebase related commands

Merged branch content:

git rebase [branch]

Continue to merge after resolving the conflict:

During the rebase process, there may be conflicts. In this case, Git will stop the rebase and let you resolve the conflicts; after the conflicts are resolved, use the "git-add" command to update the index of these contents (index), then, you don't need to execute git-commit, just execute:

git rebase --continue

Terminate the merge (restore to the original state): at any time, you can use the -abort parameter to terminate the rebase operation

git rebase --abort

The difference between stash and rebase

First, the principle is different

The stash operation only saves the modified content, and then needs to perform a merge operation to merge the latest commit of the master into the branch branch, and then execute stash apply to restore the temporarily stored modified content, so that commit4 points to the latest The content has been revised.
The rebase operation means that the modified content has been submitted to the local branch in the branch branch. At this time, the modified content will be put into the patch first, and then restored to the unmodified state, and then the latest modification of the master will be merged to In the branch, then restore the content in the patch, and also point commit4 to the latest content modification.

Second, the timing is different

Stash operation, modified content, has not been submitted to the local branch, nor submitted to the remote branch
rebase operation, modified content, has been submitted to the local branch, not submitted to the remote branch

Third, the steps are different

Stash operation, need to perform stash + merge + apply
rebase operation, need to perform commit + rebase

Reference: https://www.jianshu.com/p/3dc4677a3b08

Guess you like

Origin blog.csdn.net/p715306030/article/details/113407259