企业公司网站制作,网站是什么时候开始的,网站排名快速见效的方法,电商网站流程图git rebase 命令简介
git rebase命令允许我们轻松地更改一系列提交#xff0c;修改存储库的历史记录。我们可以重新排序、编辑或合并提交。一般常用git rebase来合并当前分支的多个commit记录#xff08;压缩#xff09;以及避免出现分支的交叉合并#xff08;变基#x…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 basetouch 1.txt
git add .
git commit -m add 1touch 2.txt
git add .
git commit -m add 2touch 3.txt
git add .
git commit -m add 3touch 4.txt
git add .
git commit -m add 4touch 5.txt
git add .
git commit -m add 5## 查看现在的提交
git log
commit 24b96811274886be653492b3afb9434f0a6a8c4f (HEAD - master)
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:36:13 2023 0800add 5commit 8117d20aa00dae8d4e71f835bba716e7cf8aec83
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:35:42 2023 0800add 4commit 4e8153308b8d71e89eddb6759881c4dd8838d2d9
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:35:09 2023 0800add 3commit ba2d4a8dd4976e63903f8e7777dbead108c5dbcb
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:34:50 2023 0800add 2commit 7d28548e7418f98f385edba1ef667cf7508d1e82
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:34:25 2023 0800add 1commit 9d6189ffbbf3da34e33d24b5058a155f3d1f5bda
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:33:51 2023 0800add basepick命令演示-更改提交顺序删除提交 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 commits 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 commits
# . 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 JiuWuyouAbc.com
Date: Fri Oct 20 14:35:42 2023 0800add 4commit 40fecbc61d8d318d23b856e5e075600667dc9fdc
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:36:13 2023 0800add 5commit 4e8153308b8d71e89eddb6759881c4dd8838d2d9
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:35:09 2023 0800add 3假设我们紧接着要删除add 5这次提交该如何做
我们只需要在弹出的交互式窗口中删除如下这一行保存退出即可
pick 24b9681 add 5使用git log查看结果如下add 5的提交被删除成功
$ git log
commit 958d1bc05005f0141815e76f498406f443912e8d (HEAD - master)
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:35:42 2023 0800add 4commit 4e8153308b8d71e89eddb6759881c4dd8838d2d9
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:35:09 2023 0800add 3commit ba2d4a8dd4976e63903f8e7777dbead108c5dbcb
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:34:50 2023 0800add 2commit 7d28548e7418f98f385edba1ef667cf7508d1e82
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:34:25 2023 0800add 1commit 9d6189ffbbf3da34e33d24b5058a155f3d1f5bda
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:33:51 2023 0800add baserecord使用该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 JiuWuyouAbc.com
Date: Fri Oct 20 14:35:42 2023 0800add 4commit 9a19efcb04106fbb029f84d7e5e36f8fabe5a763
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:35:09 2023 0800add 3commit 7c628dbc275b749e8a6d20cff13f33d55325dc07
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:34:50 2023 0800add 2 - new commitcommit 7d28548e7418f98f385edba1ef667cf7508d1e82
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:34:25 2023 0800add 1commit 9d6189ffbbf3da34e33d24b5058a155f3d1f5bda
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:33:51 2023 0800add baseedit修改提交 如果选择编辑提交将有机会修改提交这意味着可以完全添加或更改提交。还可以在继续修改之前进行更多的提交。这允许将大的提交拆分为较小的提交或者删除提交中所做的错误更改 假设我们要在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, withgit commit --amendOnce you are satisfied with your changes, rungit rebase --continueJiuWuyouABC MINGW64 /d/Code/gitopr (master|REBASE 1/2)可以看到我们的master分支多了REBASE 1/2我们尝试做一些修改给3.txt文本中增加一些内容然后提交
JiuWuyouABC MINGW64 /d/Code/gitopr (master|REBASE 1/2)
$ git add 3.txtJiuWuyouABC MINGW64 /d/Code/gitopr (master|REBASE 1/2)
$ git commit -medit 3.txt
[detached HEAD cfb4f5b] edit 3.txt1 file changed, 1 insertion()紧接着我们继续rebase
JiuWuyouABC MINGW64 /d/Code/gitopr (master|REBASE 1/2)
$ git rebase --continue
Successfully rebased and updated refs/heads/master.JiuWuyouABC MINGW64 /d/Code/gitopr (master)根据上述信息rebase成功然后我们使用git log查看结果如下成功在add 3和add 4之间提交了一次新的提交
$ git log
commit c0b72762408e0d28a914dcae98ef5c41ff6ff662 (HEAD - master)
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:35:42 2023 0800add 4commit cfb4f5b8ab1fb6794c0e219366bd9b4eb625d91f
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 15:06:27 2023 0800edit 3.txtcommit 9a19efcb04106fbb029f84d7e5e36f8fabe5a763
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:35:09 2023 0800add 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即可
JiuWuyouABC MINGW64 /d/Code/gitopr (master|REBASE 1/2)
$ git add 3.txtJiuWuyouABC MINGW64 /d/Code/gitopr (master|REBASE 1/2)
$ git commit --amend
[detached HEAD c1afb8d] edit 3.txtDate: Fri Oct 20 15:06:27 2023 08001 file changed, 2 insertions()JiuWuyouABC MINGW64 /d/Code/gitopr (master|REBASE 1/2)
$ git rebase --continue
Successfully rebased and updated refs/heads/master.JiuWuyouABC MINGW64 /d/Code/gitopr (master)
$ git log
commit 4bdef431d96b06ccf128cc71647f77bfffc7bc9e (HEAD - master)
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:35:42 2023 0800add 4commit c1afb8db039a9ca3f19862561fb282682ff51095
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 15:06:27 2023 0800edit 3.txtsquash合并提交 该命令允许将两个或多个提交合并为单个提交。一个提交被压缩到它上面的提交中。Git给用户机会编写描述这两个更改的新提交消息。 $ git log
commit 4bdef431d96b06ccf128cc71647f77bfffc7bc9e (HEAD - master)
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:35:42 2023 0800add 4commit c1afb8db039a9ca3f19862561fb282682ff51095
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 15:06:27 2023 0800edit 3.txtcommit 9a19efcb04106fbb029f84d7e5e36f8fabe5a763
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:35:09 2023 0800add 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 JiuWuyouAbc.com
Date: Fri Oct 20 15:06:27 2023 0800edit 3.txtadd 4commit 9a19efcb04106fbb029f84d7e5e36f8fabe5a763
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:35:09 2023 0800add 3fixup将该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 JiuWuyouAbc.com
Date: Fri Oct 20 14:35:09 2023 0800add 3commit 7c628dbc275b749e8a6d20cff13f33d55325dc07
Author: JiuWuyou JiuWuyouAbc.com
Date: Fri Oct 20 14:34:50 2023 0800add 2 - new commitexec 执行shell命令
git rebase -i HEAD~3pick 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的操作要多多练习练习中不要怕出错 参考git rebase的压缩与变基 https://blog.csdn.net/qgccdd061313/article/details/128675617 ↩︎