git操作指南-git-rebase

参考文章:
连猴子都能懂的Git入门指南-使用 rebase 合并

廖雪峰:git rebase

https://git-scm.com/docs/git-rebase

https://git-scm.com/book/en/v2/Git-Branching-Rebasing

https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase

Git rebase

什么是 git rebase?

Rebasing is the process of moving or combining a sequence of commits to a new base commit. Rebasing is most useful and easily visualized in the context of a feature branching workflow.

rebase 即 “变基”。

将提交到某一分支上的所有修改都移至另一分支上。

image-20220531130625465

From a content perspective, rebasing is changing the base of your branch from one commit to another making it appear as if you’d created your branch from a different commit. Internally, Git accomplishes this by creating new commits and applying them to the specified base. It’s very important to understand that even though the branch looks the same, it’s composed of entirely new commits.

例如:

$ git checkout experiment
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: added staged command

原理是首先找到这两个分支(即当前分支 experiment、变基操作的目标基底分支 master)的最近共同祖先 C2,然后对比当前分支相对于该祖先的历次提交,提取相应的修改并存为临时文件,然后将当前分支指向目标基底 C3, 最后以此将之前另存为临时文件的修改依序应用。

image-20220531132016396

Git rebase 会将提交历史变成一条直线。使提交记录变得更加的整洁。

使用 git rebase 之前
image-20220531135532739

使用命令

git rebase second

使用 git rebase 之后

image-20220531135810246

这样就能得到一个干净的提交记录了。

git rebase 和 git merge 的操作在提交记录的显示上也有很大的不同。

开发模式下

如何使用?

与 git merge 的不同