rebase使用技巧
gml

变基时可用的命令

变基时有六个命令可用:

  • pick

pick只表示包含提交。 在变基进行时重新排列pick命令的顺序会更改提交的顺序。 如果选择不包含提交,应删除整行。

  • reword

reword命令类似于pick,但在使用后,变基过程就会暂停,让你有机会改变提交消息。 提交所做的任何更改都不受影响。

  • edit

如果选择 edit提交,你将有机会修订提交,也就是说,可以完全添加或更改提交。 您也可以创建更多提交后再继续变基。 这样您可以将大提交拆分为小提交,或者删除在提交中执行错误更改。

  • squash

此命令可用于将两个或以上的提交合并为一个。 下面的提交压缩到其上面的提交。 Git 让您有机会编写描述两次更改的新提交消息。

  • fixup

这类似于 squash,但要合并的提交丢弃了其消息。 提交只是合并到其上面的提交,之前提交的消息用于描述两次更改。

  • exec

这可让您对提交运行任意shell命令。

更改提交顺序

pick只是意味着包括提交。重新进行命令时,重新安排pick`命令的顺序会更改提交的顺序。如果选择不包括提交,则应删除整行。
我们先看一下当前提交的信息

image

现在我们要改变一下bd两次提交的顺序,HRAD~2表示选择离HEAD最近的3次提交。

1
git rebase -i HEAD~3

接着git会弹出一个文本,
image
下面就是vim操作了,只要交换第一行和第三行的位置就行,把光标移动到第一行快速按两下d键第一行就被剪切到剪切板中了,再把光标移动到d提交那行按下p就把刚才剪切的内容粘贴过来了,像这样操作把191bc18那次提交移动到第一行就行了,按下shift + :输入wq保存退出。
输入git log查看提交信息顺序改变了。
image

删除提交

比如我们要删除提交信息为c的那次提交,先看一下当前
image
HEAD~2距离HEAD最近的两次提交

1
git rebase -i HEAD~2

只要删除提交信息为c的那行就行了。
image
保存后输入git log查看结果
image

record 修改提交消息(提交内容不变)

如果我们要修改b的那次提交的commit信息,可以使用record来修改commit信息,输入git log先看一下现在的commit是什么。
image
c37146f7可以快速定位到c37146f7 后面提交的地方,但是列出的不包括指定的提交。

1
git rebase -i c37146f7

pack定改为r,r是 record简写。
image
接着Esc,shift + ;``wq 保存退出,git会弹出一个文本编辑器在第一行修改文本描述。
image
修改完成后保存退出,输入git log查看commit信息。
image

edit修改提交

使用edit可以完全添加或更改提交。您还可以进行更多提交,然后再继续进行变基。这使您可以将大型提交拆分为较小的提交,或者删除在提交中所做的错误更改。
如果在3d06118cf18b62266之间在添加一个提交要怎么做呢,
显示到HEAD最近到两次提交

1
git rebase -i HEAD~2
1
2
3
4
5
6
7
8
9
10
11
12
13
pick c37146f d
pick 3d06118 修改commit b

# Rebase f18b622..3d06118 onto f18b622 (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit

c37146f前面的pick修改为e,保存并退后后git会输出一下内容

1
2
3
4
5
6
7
8
Stopped at c37146f...  d
You can amend the commit now, with

git commit --amend

Once you are satisfied with your changes, run

git rebase --continue

此时可以看到master变成了(master|REBASE 1/2)
新建一个c.txt文件并提交。

1
2
3
4
5
git add c.txt
git commit -m "c"
[detached HEAD c66dc69] c
1 file changed, 1 insertion(+)
create mode 100644 c.txt

接着继续rebase

1
2
git rebase --continue
Successfully rebased and updated refs/heads/master.

再次查看一下提交记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
git log

commit 2fc98daaf7552d7dbc8e9c078ad205026344674f (HEAD -> master)
Author: you <youemail@outlook.com>
Date: Thu Sep 8 13:13:35 2022 +0800

修改commit b

commit c66dc69744c255f4827da269e92d8228f8ebd737
Author: you <youemail@outlook.com>
Date: Fri Sep 9 10:26:37 2022 +0800

c

commit c37146f70d8cc1632f818d2ea013a34550a1c792
Author: you <youemail@outlook.com>
Date: Thu Sep 8 13:13:43 2022 +0800

d

commit f18b62266b664cd3228bd332f81a32dcd6a1ad1f
Author: you <youemail@outlook.com>
Date: Thu Sep 8 13:13:30 2022 +0800

a

如果我们只想修改提交的内容,不添加commit要怎么办
参考上面的步骤在提交时加一个参数git commit --amend这样就不会多一个commit了。

1
2
3
4
git add c.txt
git commit --amend
>
Successfully rebased and updated refs/heads/master.

squash合并提交

squash可以将两个或多个commit合并到一个commit中,被合并的commit会压缩到上一次的commit中,还可以更改这两个commit合并后新的commit信息。
如果我们要合并e78c2233437126要怎么做呢,先看一下当前的提交信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Author: you <youemail@outlook.com>
Date: Thu Sep 8 13:13:35 2022 +0800

修改commit b

commit 34371265e7df46cd5b877339ad4d1b3a4d8a315e
Author: you <youemail@outlook.com>
Date: Fri Sep 9 10:26:37 2022 +0800

c

commit c37146f70d8cc1632f818d2ea013a34550a1c792
Author: you <youemail@outlook.com>
Date: Thu Sep 8 13:13:43 2022 +0800

d

commit f18b62266b664cd3228bd332f81a32dcd6a1ad1f
Author: you <youemail@outlook.com>
Date: Thu Sep 8 13:13:30 2022 +0800

a
(END)
1
2
3
4
5
6
7
8
9
10
git rebase - i HEAD~2
>
pick 3437126 c
pick e78c223 修改commit b

# Rebase c37146f..e78c223 onto c37146f (2 commands)
#
# Commands:
# p, pick <commit> = use commit
...

因为需要将e78c223合并到他的上次提交,需要把e78c223前面的pick改为s

1
2
3
4
5
6
7
8
9
10
git rebase - i HEAD~2
>
pick 3437126 c
s e78c223 修改commit b

# Rebase c37146f..e78c223 onto c37146f (2 commands)
#
# Commands:
# p, pick <commit> = use commit
...

保存并退出,git会弹出新的文本框

1
2
3
4
5
6
7
8
9
10
11
# This is a combination of 2 commits.
# This is the 1st commit message:

c

# This is the commit message #2:

修改commit b

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.

修改commit信息

1
2
3
4
5
6
7
8
9
10
11
# This is a combination of 2 commits.
# This is the 1st commit message:

新的commit c

# This is the commit message #2:

新的commit 修改commit b

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.

保存并退出,变基完成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
git log
>
commit 88a2a47912f3fe643d486739b000dfae776ed493 (HEAD -> master)
Author: you <youeamil@outlook.com>
Date: Fri Sep 9 10:26:37 2022 +0800

新的commit c

新的commit 修改commit b

commit c37146f70d8cc1632f818d2ea013a34550a1c792
Author: you <youeamil@outlook.com>
Date: Thu Sep 8 13:13:43 2022 +0800

d

commit f18b62266b664cd3228bd332f81a32dcd6a1ad1f
Author: you <youeamil@outlook.com>
Date: Thu Sep 8 13:13:30 2022 +0800

a

常看commit修改 git show 88a2a47

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
git show 88a2a47
>
commit 88a2a47912f3fe643d486739b000dfae776ed493 (HEAD -> master)
Author: you <youemail@outlook.com>
Date: Fri Sep 9 10:26:37 2022 +0800

新的commit c

新的commit 修改commit b

diff --git a/b.txt b/b.txt
new file mode 100644
index 0000000..63d8dbd
--- /dev/null
+++ b/b.txt
@@ -0,0 +1 @@
+b
\ No newline at end of file
diff --git a/c.txt b/c.txt
new file mode 100644
index 0000000..d36cf97
--- /dev/null
+++ b/c.txt
@@ -0,0 +1 @@
+ccccc
\ No newline at end of file

fixup合并提交,只保留较早的提交信息

使用fixup会把相邻的commit合并到上一次的commit中,会保留上次的commit信息,fixup不可以编辑commit信息
查看距离HEAD最近的两次提交

1
2
3
4
5
6
git rebase -i HEAD~2
>
pick c37146f d
pick 88a2a47 新的commit c

# Rebase f18b622..88a2a47 onto f18b622 (2 commands)

88a2a47前面pick改为f88a2a47修改的内容就会合到c37146f上面.

1
2
3
4
pick c37146f d
f 88a2a47 新的commit c

# Rebase f18b622..88a2a47 onto f18b622 (2 commands)
1
2
3
4
5
6
7
8
9
10
11
12
13
git log
>
commit c6dcdbc2c174b6480aa6d76ca3cfa3ec5b6a7ea5 (HEAD -> master)
Author: you <youeamil@outlook.com>
Date: Thu Sep 8 13:13:43 2022 +0800

d

commit f18b62266b664cd3228bd332f81a32dcd6a1ad1f
Author: you <youeamil@outlook.com>
Date: Thu Sep 8 13:13:30 2022 +0800

a
由 Hexo 驱动 & 主题 Keep
访客数 访问量