git rebase 下令简介
git rebase下令答应我们轻松地更改一系列提交,修改存储库的汗青记录。我们可以重新排序、编辑或合并提交。一般常用git rebase来合并当前分支的多个commit记录(压缩)以及制止出现分支的交织合并(变基)1
git rebase 可用的下令
下令缩写表明pickp保留使用该commit。重新安排pick下令的次序会改变提交的次序。如果选择不包罗提交,则应该删除整行。rewordr使用该commit但必要编辑。雷同于pick,但是在使用它之后,rebase进程将停息,并给您一个修改提交消息的时机。提交所做的任何更改都不受影响。edite使用该commit但必要停下来修改该提交。如果选择编辑提交,将有时机修改提交,这意味着可以完全添加或更改提交。还可以在继承修改之前进行更多的提交。这答应将大的提交拆分为较小的提交,或者删除提交中所做的错误更改。squashs将该commit合并到前一个commit。该下令答应将两个或多个提交合并为单个提交。一个提交被压缩到它上面的提交中。Git给用户时机编写描述这两个更改的新提交消息。fixupf将该commit合并到前一个commit,不必要保留该commit的注释。这与squash雷同,但是要合并的提交会丢弃其消息。提交简朴地合并到它上面的提交中,并且早先的提交的消息用于描述这两个更改。execx使用shell实行下令dropd删除提交 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交互式窗口弹出等待进一步操作,如下是上面下令实行后的弹框
- 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这次提交,该怎样做?
我们只必要在弹出的交互式窗口中删除如下这一行,保存退出即可
使用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次,则我们要使用下面的下令
弹出如下信息:
- 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修改如下
保存退出后,使用git log检察发现add 2的提交信息乐成修改了
- commit bec01eba90140cd0158465dbec1b49aed2a183ff (HEAD -> master)Author: JiuWuyou <JiuWuyou@Abc.com>Date: Fri Oct 20 14:35:42 2023 +0800 add 4commit 9a19efcb04106fbb029f84d7e5e36f8fabe5a763Author: JiuWuyou <JiuWuyou@Abc.com>Date: Fri Oct 20 14:35:09 2023 +0800 add 3commit 7c628dbc275b749e8a6d20cff13f33d55325dc07Author: JiuWuyou <JiuWuyou@Abc.com>Date: Fri Oct 20 14:34:50 2023 +0800 add 2 - new commit
- commit 7d28548e7418f98f385edba1ef667cf7508d1e82Author: JiuWuyou <JiuWuyou@Abc.com>Date: Fri Oct 20 14:34:25 2023 +0800 add 1commit 9d6189ffbbf3da34e33d24b5058a155f3d1f5bdaAuthor: JiuWuyou <JiuWuyou@Abc.com>Date: Fri Oct 20 14:33:51 2023 +0800 add base
复制代码 edit修改提交
如果选择编辑提交,将有时机修改提交,这意味着可以完全添加或更改提交。还可以在继承修改之前进行更多的提交。这答应将大的提交拆分为较小的提交,或者删除提交中所做的错误更改
假设我们要在add 3和add 4两次提交之间再加提交,该怎样操作?
我们先输入如下下令
弹出如下信息
- 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 3You can amend the commit now, with git commit --amendOnce 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.txtJiuWuyou@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 logcommit 4bdef431d96b06ccf128cc71647f77bfffc7bc9e (HEAD -> master)Author: JiuWuyou <JiuWuyou@Abc.com>Date: Fri Oct 20 14:35:42 2023 +0800 add 4commit c1afb8db039a9ca3f19862561fb282682ff51095Author: 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这两次提交合并,我们该怎么操作?
再弹出的交互窗口中将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的提交信息,该怎样操作?
弹出如下信息
- 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下令
- pick 7d28548 add 1pick 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 1pick 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的操作要多多练习,练习中不要怕出错
- 参考git rebase的压缩与变基 https://blog.csdn.net/qgccdd061313/article/details/128675617 ↩︎
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |