git rebase -i 详解

这篇具有很好参考价值的文章主要介绍了git rebase -i 详解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

git的操作要多多练习,练习中不要怕出错

  1. 参考git rebase的压缩与变基 https://blog.csdn.net/qgccdd061313/article/details/128675617 ↩︎文章来源地址https://www.toymoban.com/news/detail-763838.html

到了这里,关于git rebase -i 详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • git rebase与git merge图文详解(一文看懂区别)

    git rebase与git merge图文详解(一文看懂区别)

    大家在工作中团队开发的时候对于拉取分支和合并代码时就会涉及到两种选择,git rebase与git merge: rebase:变基,会有一个干净的分支,但是对于记录来源不够清晰 merge:合并,git分支看起来比较混乱,但是清楚各个记录的来源与时间节点 推荐:全部使用merge 拉公共分支使用

    2024年01月18日
    浏览(16)
  • IDEA git 操作中的Merge和Rebase

    IDEA git 操作中的Merge和Rebase

    \\\"Merge incoming changes into the current branch\\\"和\\\"Rebase the current branch on top of incoming changes\\\"都是在Git中常用的合并分支的方式。 它们的主要区别在于合并后的分支历史记录和代码改动的顺序不同。 \\\"Merge incoming changes into the current branch\\\"会将远程分支的改动合并到当前分支的新提交中。这

    2024年02月16日
    浏览(16)
  • 【git merge/rebase】详解合并代码、解决冲突

    【git merge/rebase】详解合并代码、解决冲突

    目录 1.概述 2.merge 3.rebase 4.merge和rabase的区别 5.解决冲突 在实际开发中,一个项目往往是多个人一起协作的,头天下班前大家把代码交到远端仓库,第二天工作的第一件事情都是从服务器上拉最新的代码,保证代码版本的一致性。在这种团队协作中大家修改到同一份文件是难

    2024年02月08日
    浏览(44)
  • git rebase (合并代码和整理提交记录)图文详解

    git rebase (合并代码和整理提交记录)图文详解

    建议在看这篇文章之前一定要看完:git reset 命令详解 git revert命令详解。 看完上面的文章后,在rebase操作(成功/失误)后还可以进行回退。不至于咱们再去费劲创建那些提交记录。 git rebase 有两种作用 合并代码 整理提交记录 可以看到有两个分支,2023的分支是在master的基础

    2024年02月09日
    浏览(15)
  • 【Git】Git 操作命令可视化(五):git clone、git fetch、git pull、git push、git pull --rebase、解决远程仓库与本地仓库的代码冲突

    【Git】Git 操作命令可视化(五):git clone、git fetch、git pull、git push、git pull --rebase、解决远程仓库与本地仓库的代码冲突

    1. git clone main是本地的main分支,o(origin)/main是表示本地拉去下来的远程的main分支 o/main分支记录了远程仓库拉取时的分支状态 远程分支有一个特别的属性,在你切换到远程分支时,git会自动进入分离 HEAD 状态(这样做是因为git不想让你在本地就能直接进行修改远程仓库代码的

    2024年02月08日
    浏览(117)
  • Git:git merge和git rebase的区别

    Git:git merge和git rebase的区别

    git merge是用来合并两个分支的。比如:将 b 分支合并到当前分支。同样git rebase b,也是把 b 分支合并到当前分支。他们的 「原理」如下: 假设你现在基于远程分支\\\"origin\\\",创建一个叫\\\"mywork\\\"的分支。 $ git checkout -b mywork origin 假设远程分支\\\"origin\\\"已经有了 2 个提交,如下。 现在

    2024年02月16日
    浏览(13)
  • git fetch, git pull, git merge, git rebase

    git fetch, git pull, git merge, git rebase

    背景: 我们发现很多同学习惯性用git pull拉取远端仓库代码, 但是这会带来一个问题,如果有了本地commit再git pull的话,会产生一个merge commit, 这样的merge commit会导致git log分支节点很多,很乱。 首先聊一聊git pull,git merge,git rebase这些常听到的命令吧,初入git的同学一定还

    2024年02月03日
    浏览(14)
  • git merge 和git rebase的区别

    git merge 和git rebase的区别

    在Git版本控制系统中,有两种方式可以将一个分支的更改合并到另一个分支: git merge 和 git rebase 。虽然它们都可以完成相同的任务,但它们的实现方式有所不同,并且在使用时需要谨慎考虑。 git merge 将两个分支的更改合并到一起,形成一个新的提交节点,称为合并提交。这

    2024年02月12日
    浏览(14)
  • 0065__git fetch, git pull, git merge, git rebase

    git fetch, git pull, git merge, git rebase_git pull和merge_送你一朵小莲花的博客-CSDN博客

    2024年02月09日
    浏览(13)
  • 【随笔】Git 基础篇 -- 分支与合并 git rebase(十)

    【随笔】Git 基础篇 -- 分支与合并 git rebase(十)

    💌 所属专栏:【Git】 😀 作  者:我是夜阑的狗🐶 🚀 个人简介:一个正在努力学技术的CV工程师,专注基础和实战分享 ,欢迎咨询! 💖 欢迎大家:这里是CSDN,我总结知识的地方,喜欢的话请三连,有问题请私信 😘 😘 😘 您的点赞、关注、收藏、评论,是对我最大

    2024年04月14日
    浏览(14)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包