git rebase 命令简介
git rebase命令允许我们轻松地更改一系列提交,修改存储库的历史记录。我们可以重新排序、编辑或合并提交。一般常用git rebase来合并当前分支的多个commit记录(压缩)以及避免出现分支的交叉合并(变基)1
git rebase 可用的命令
命令 | 缩写 | 解释 |
---|---|---|
pick | p | 保留使用该commit。重新安排pick命令的顺序会改变提交的顺序。如果选择不包含提交,则应该删除整行。 |
reword | r | 使用该commit但需要编辑。类似于pick,但是在使用它之后,rebase进程将暂停,并给您一个修改提交消息的机会。提交所做的任何更改都不受影响。 |
edit | e | 使用该commit但需要停下来修改该提交。如果选择编辑提交,将有机会修改提交,这意味着可以完全添加或更改提交。还可以在继续修改之前进行更多的提交。这允许将大的提交拆分为较小的提交,或者删除提交中所做的错误更改。 |
squash | s | 将该commit合并到前一个commit。该命令允许将两个或多个提交合并为单个提交。一个提交被压缩到它上面的提交中。Git给用户机会编写描述这两个更改的新提交消息。 |
fixup | f | 将该commit合并到前一个commit,不需要保留该commit的注释。这与squash类似,但是要合并的提交会丢弃其消息。提交简单地合并到它上面的提交中,并且早先的提交的消息用于描述这两个更改。 |
exec | x | 使用shell执行命令 |
drop | d | 删除提交 |
git rebase 各个命令实操
实操前的准备
# 我们初始化一个项目
git init
## 制造一些提交
touch base.txt
git add .
git commit -m "add base"
touch 1.txt
git add .
git commit -m "add 1"
touch 2.txt
git add .
git commit -m "add 2"
touch 3.txt
git add .
git commit -m "add 3"
touch 4.txt
git add .
git commit -m "add 4"
touch 5.txt
git add .
git commit -m "add 5"
## 查看现在的提交
git log
commit 24b96811274886be653492b3afb9434f0a6a8c4f (HEAD -> master)
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:36:13 2023 +0800
add 5
commit 8117d20aa00dae8d4e71f835bba716e7cf8aec83
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:35:42 2023 +0800
add 4
commit 4e8153308b8d71e89eddb6759881c4dd8838d2d9
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:35:09 2023 +0800
add 3
commit ba2d4a8dd4976e63903f8e7777dbead108c5dbcb
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:34:50 2023 +0800
add 2
commit 7d28548e7418f98f385edba1ef667cf7508d1e82
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:34:25 2023 +0800
add 1
commit 9d6189ffbbf3da34e33d24b5058a155f3d1f5bda
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:33:51 2023 +0800
add base
pick命令演示-更改提交顺序,删除提交
pick保留使用该commit。重新安排pick命令的顺序会改变提交的顺序。如果选择不包含提交,则应该删除整行。
假设我们现在要改变已提交的5.txt和4.txt的顺序,该如何操作?
第一步:告诉git我要操作最近的2次提交
git rebase -i HEAD~2
接着,git交互式窗口弹出等待进一步操作,如下是上面命令执行后的弹框
pick 8117d20 add 4
pick 24b9681 add 5
# Rebase 4e81533..24b9681 onto 4e81533 (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
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
第二步:把第一行和第二行交换顺序,即如下顺序,然后在vim编辑器中保存
pick 24b9681 add 5
pick 8117d20 add 4
然后使用git log查看内容如下,成功
$ git log
commit 360558534b8b79dafdd77131485b252a0ad3bdd6 (HEAD -> master)
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:35:42 2023 +0800
add 4
commit 40fecbc61d8d318d23b856e5e075600667dc9fdc
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:36:13 2023 +0800
add 5
commit 4e8153308b8d71e89eddb6759881c4dd8838d2d9
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:35:09 2023 +0800
add 3
假设我们紧接着要删除add 5这次提交,该如何做?
我们只需要在弹出的交互式窗口中删除如下这一行,保存退出即可
pick 24b9681 add 5
使用git log查看结果如下,add 5的提交被删除,成功
$ git log
commit 958d1bc05005f0141815e76f498406f443912e8d (HEAD -> master)
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:35:42 2023 +0800
add 4
commit 4e8153308b8d71e89eddb6759881c4dd8838d2d9
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:35:09 2023 +0800
add 3
commit ba2d4a8dd4976e63903f8e7777dbead108c5dbcb
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:34:50 2023 +0800
add 2
commit 7d28548e7418f98f385edba1ef667cf7508d1e82
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:34:25 2023 +0800
add 1
commit 9d6189ffbbf3da34e33d24b5058a155f3d1f5bda
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:33:51 2023 +0800
add base
record使用该commit并编辑(提交内容不变)
record类似于pick,但是在使用它之后,rebase进程将暂停,并给您一个修改提交消息的机会。提交所做的任何更改都不受影响
假设我们要修改add 2的提交comment信息,该怎么操作?
使用git log查看add 2的提交属于最近提交的倒数第3次,则我们要使用下面的命令
git rebase -i HEAD~3
弹出如下信息:
pick ba2d4a8 add 2
pick 4e81533 add 3
pick 958d1bc add 4
# Rebase 7d28548..958d1bc onto 7d28548 (3 commands)
#
我们只需要修改第一行的add 2如下:(r ba2d4a8 add 2或record ba2d4a8 add 2都可以),然后保存退出
r ba2d4a8 add 2
pick 4e81533 add 3
pick 958d1bc add 4
# Rebase 7d28548..958d1bc onto 7d28548 (3 commands)
#
紧接着弹出如下交互界面
add 2
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Fri Oct 20 14:34:50 2023 +0800
#
# interactive rebase in progress; onto 7d28548
# Last command done (1 command done):
# reword ba2d4a8 add 2
# Next commands to do (2 remaining commands):
# pick 4e81533 add 3
# pick 958d1bc add 4
# You are currently editing a commit while rebasing branch 'master' on '7d28548'.
#
# Changes to be committed:
# new file: 2.txt
我们现在就可以修改add 2这个提交信息了,我们将第一行add 2修改如下
add 2 - new commit
保存退出后,使用git log查看发现add 2的提交信息成功修改了
commit bec01eba90140cd0158465dbec1b49aed2a183ff (HEAD -> master)
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:35:42 2023 +0800
add 4
commit 9a19efcb04106fbb029f84d7e5e36f8fabe5a763
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:35:09 2023 +0800
add 3
commit 7c628dbc275b749e8a6d20cff13f33d55325dc07
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:34:50 2023 +0800
add 2 - new commit
commit 7d28548e7418f98f385edba1ef667cf7508d1e82
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:34:25 2023 +0800
add 1
commit 9d6189ffbbf3da34e33d24b5058a155f3d1f5bda
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:33:51 2023 +0800
add base
edit修改提交
如果选择编辑提交,将有机会修改提交,这意味着可以完全添加或更改提交。还可以在继续修改之前进行更多的提交。这允许将大的提交拆分为较小的提交,或者删除提交中所做的错误更改
假设我们要在add 3和add 4两次提交之间再加提交,该如何操作?
我们先输入如下命令
git rebase -i HEAD~2
弹出如下信息
pick 9a19efc add 3
pick bec01eb add 4
# Rebase 7c628db..bec01eb onto 7c628db (2 commands)
修改如下
e 9a19efc add 3
pick bec01eb add 4
# Rebase 7c628db..bec01eb onto 7c628db (2 commands)
保存退出后,git反馈如下信息
$ git rebase -i HEAD~2
Stopped at 9a19efc... add 3
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
JiuWuyou@ABC MINGW64 /d/Code/gitopr (master|REBASE 1/2)
可以看到我们的master分支多了REBASE 1/2,我们尝试做一些修改,给3.txt文本中增加一些内容,然后提交
JiuWuyou@ABC MINGW64 /d/Code/gitopr (master|REBASE 1/2)
$ git add 3.txt
JiuWuyou@ABC MINGW64 /d/Code/gitopr (master|REBASE 1/2)
$ git commit -m"edit 3.txt"
[detached HEAD cfb4f5b] edit 3.txt
1 file changed, 1 insertion(+)
紧接着我们继续rebase
JiuWuyou@ABC MINGW64 /d/Code/gitopr (master|REBASE 1/2)
$ git rebase --continue
Successfully rebased and updated refs/heads/master.
JiuWuyou@ABC MINGW64 /d/Code/gitopr (master)
根据上述信息,rebase成功,然后我们使用git log查看,结果如下,成功在add 3和add 4之间提交了一次新的提交
$ git log
commit c0b72762408e0d28a914dcae98ef5c41ff6ff662 (HEAD -> master)
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:35:42 2023 +0800
add 4
commit cfb4f5b8ab1fb6794c0e219366bd9b4eb625d91f
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 15:06:27 2023 +0800
edit 3.txt
commit 9a19efcb04106fbb029f84d7e5e36f8fabe5a763
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:35:09 2023 +0800
add 3
假设我们要单独的修改edit 3.txt这次提交内容和消息,该如何操作?
我们先使用git rebase -i HEAD~2(因为edit 3.txt是倒数第2次提交),然后将edit 3.txt前的pick改为e保存退出后,继续执行下面步骤
修改3.txt文本内容,然后执行git add 3.txt后执行git commit --amend去修改信息,然后使用git rebase --continue即可
JiuWuyou@ABC MINGW64 /d/Code/gitopr (master|REBASE 1/2)
$ git add 3.txt
JiuWuyou@ABC MINGW64 /d/Code/gitopr (master|REBASE 1/2)
$ git commit --amend
[detached HEAD c1afb8d] edit 3.txt
Date: Fri Oct 20 15:06:27 2023 +0800
1 file changed, 2 insertions(+)
JiuWuyou@ABC MINGW64 /d/Code/gitopr (master|REBASE 1/2)
$ git rebase --continue
Successfully rebased and updated refs/heads/master.
JiuWuyou@ABC MINGW64 /d/Code/gitopr (master)
$ git log
commit 4bdef431d96b06ccf128cc71647f77bfffc7bc9e (HEAD -> master)
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:35:42 2023 +0800
add 4
commit c1afb8db039a9ca3f19862561fb282682ff51095
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 15:06:27 2023 +0800
edit 3.txt
squash合并提交
该命令允许将两个或多个提交合并为单个提交。一个提交被压缩到它上面的提交中。Git给用户机会编写描述这两个更改的新提交消息。
$ git log
commit 4bdef431d96b06ccf128cc71647f77bfffc7bc9e (HEAD -> master)
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:35:42 2023 +0800
add 4
commit c1afb8db039a9ca3f19862561fb282682ff51095
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 15:06:27 2023 +0800
edit 3.txt
commit 9a19efcb04106fbb029f84d7e5e36f8fabe5a763
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:35:09 2023 +0800
add 3
假设我们要将add 4和edit 3.txt这两次提交合并,我们该怎么操作?
git rebase -i HEAD~2
再弹出的交互窗口中将add 4前的pick修改为s,如下
pick c1afb8d edit 3.txt
s 4bdef43 add 4
保存退出后,git弹出窗口允许我们修改提交信息,默认是两个提交消息合并,我们可以啥都不改,保存退出
# This is a combination of 2 commits.
# This is the 1st commit message:
edit 3.txt
# This is the commit message #2:
add 4
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Fri Oct 20 15:06:27 2023 +0800
#
# interactive rebase in progress; onto 9a19efc
# Last commands done (2 commands done):
# pick c1afb8d edit 3.txt
# squash 4bdef43 add 4
# No commands remaining.
# You are currently rebasing branch 'master' on '9a19efc'.
#
# Changes to be committed:
# modified: 3.txt
# new file: 4.txt
使用git log查询如下,合并成功
$ git log
commit 60b3ebdd3cb11fbcba3aaa33911c13881532dce1 (HEAD -> master)
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 15:06:27 2023 +0800
edit 3.txt
add 4
commit 9a19efcb04106fbb029f84d7e5e36f8fabe5a763
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:35:09 2023 +0800
add 3
fixup将该commit合并到前一个commit,不需要保留该commit的注释
与squash类似,但是要合并的提交会丢弃其消息。提交简单地合并到它上面的提交中,并且早先的提交的消息用于描述这两个更改。
假设我们要将edit 3.txt的提交内容与add 3合并,并丢弃edit 3.txt的提交信息,该如何操作?
git rebase -i HEAD~2
弹出如下信息
pick 9a19efc add 3
pick 60b3ebd edit 3.txt
# Rebase 7c628db..60b3ebd onto 7c628db (2 commands)
更改为
pick 9a19efc add 3
f 60b3ebd edit 3.txt
# Rebase 7c628db..60b3ebd onto 7c628db (2 commands)
保存退出后,使用git log查看发现edit 3.txt的提交信息消失,但edit 3.txt文本中的提交内容仍然保留着
$ git log
commit e43925beedb4520924d9996e3356040087531c3a (HEAD -> master)
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:35:09 2023 +0800
add 3
commit 7c628dbc275b749e8a6d20cff13f33d55325dc07
Author: JiuWuyou <JiuWuyou@Abc.com>
Date: Fri Oct 20 14:34:50 2023 +0800
add 2 - new commit
exec 执行shell命令
git rebase -i HEAD~3
pick 7d28548 add 1
pick 7c628db add 2 - new commit
pick e43925b add 3
# Rebase 9d6189f..e43925b onto 9d6189f (3 commands)
我们在上面的弹框内容前加一行shell命令
x echo "execute print command....."
pick 7d28548 add 1
pick 7c628db add 2 - new commit
pick e43925b add 3
# Rebase 9d6189f..e43925b onto 9d6189f (3 commands)
保存退出,shell会打印“execute print command…”内容
$ git rebase -i HEAD~3
Executing: echo "execute print command....."
execute print command.....
Successfully rebased and updated refs/heads/master.
drop
删除提交,删除不想要的提交
参考文献
[1] git 重写历史 https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History文章来源:https://www.toymoban.com/news/detail-763838.html
-
参考git rebase的压缩与变基 https://blog.csdn.net/qgccdd061313/article/details/128675617 ↩︎文章来源地址https://www.toymoban.com/news/detail-763838.html
到了这里,关于git rebase -i 详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!