讲解 Git 中的变基


变基更改了一系列提交。它将一系列提交从当前分支移至或重新放置在目标分支上。默认情况下,当前分支上不在另一个分支上的提交会被重新设定为基准。变基技术允许我们保留线性的历史记录。

让我们从下面的图表中理解这一点。

为了重新设置基准,我们需要处于要重新设置到目标中的分支中。在我们的场景中,我们需要在功能分支上执行变基命令。执行变基命令后,我们将获得一个线性的历史记录。

执行变基命令后,功能 *F1* 和 *F2* 上的提交重新定位到主分支,使其看起来好像该分支是从提交 *C3* 创建的,如下图所示。

范例

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo
$ git init
Initialized empty Git repository in E:/tut_repo/.git/

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ echo hello>hello.txt


$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git add hello.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git commit -m 'c1'
[master (root-commit) 46736ad] c1
1 file changed, 1 insertion(+)
create mode 100644 hello.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git branch feature

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git switch feature
Switched to branch 'feature'

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ echo world>world.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git add world.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git commit -m 'world'
[feature b95055e] world
1 file changed, 1 insertion(+)
create mode 100644 world.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ echo world again>>world.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git commit -am 'F2'
[feature 2d954e6] F2
1 file changed, 1 insertion(+)

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git log --oneline --all --graph
* 2d954e6 (HEAD -> feature) F2
* b95055e world
* 46736ad (master) c1

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git switch master
Switched to branch 'master'

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ echo hello again>>hello.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git commit -am 'c2'
[master c99c97a] c2
1 file changed, 1 insertion(+)

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git log --oneline --all --graph
* c99c97a (HEAD -> master) c2
| * 2d954e6 (feature) F2
| * b95055e world
|/
* 46736ad c1

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git switch feature
Switched to branch 'feature'

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git rebase master
Successfully rebased and updated refs/heads/feature.

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git log --oneline --all --graph
* 67dfc66 (HEAD -> feature) F2
* c4a8dc7 world
* c99c97a (master) c2
* 46736ad c1

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git checkout master
Switched to branch 'master'

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git log --oneline --all --graph
* 67dfc66 (feature) F2
* c4a8dc7 world
* c99c97a (HEAD -> master) c2
* 46736ad c1

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git rebase feature
Successfully rebased and updated refs/heads/master.

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git log --oneline --all --graph
* 67dfc66 (HEAD -> master, feature) F2
* c4a8dc7 world
* c99c97a c2
* 46736ad c1

更新于:2021 年 4 月 30 日

900 次浏览

开启您的 职业生涯

完成课程并获得认证

开始
广告
© . All rights reserved.