ToB企服应用市场:ToB评测及商务社交产业平台

标题: git rebase -i 详解 [打印本页]

作者: 冬雨财经    时间: 2024-7-14 07:30
标题: git rebase -i 详解
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 各个下令实操

实操前的准备
  1. # 我们初始化一个项目
  2. git init
  3. ## 制造一些提交
  4. touch base.txt
  5. git add .
  6. git commit -m "add base"
  7. touch 1.txt
  8. git add .
  9. git commit -m "add 1"
  10. touch 2.txt
  11. git add .
  12. git commit -m "add 2"
  13. touch 3.txt
  14. git add .
  15. git commit -m "add 3"
  16. touch 4.txt
  17. git add .
  18. git commit -m "add 4"
  19. touch 5.txt
  20. git add .
  21. git commit -m "add 5"
  22. ## 查看现在的提交
  23. git log
  24. commit 24b96811274886be653492b3afb9434f0a6a8c4f (HEAD -> master)
  25. Author: JiuWuyou <JiuWuyou@Abc.com>
  26. Date:   Fri Oct 20 14:36:13 2023 +0800
  27.     add 5
  28. commit 8117d20aa00dae8d4e71f835bba716e7cf8aec83
  29. Author: JiuWuyou <JiuWuyou@Abc.com>
  30. Date:   Fri Oct 20 14:35:42 2023 +0800
  31.     add 4
  32. commit 4e8153308b8d71e89eddb6759881c4dd8838d2d9
  33. Author: JiuWuyou <JiuWuyou@Abc.com>
  34. Date:   Fri Oct 20 14:35:09 2023 +0800
  35.     add 3
  36. commit ba2d4a8dd4976e63903f8e7777dbead108c5dbcb
  37. Author: JiuWuyou <JiuWuyou@Abc.com>
  38. Date:   Fri Oct 20 14:34:50 2023 +0800
  39.     add 2
  40. commit 7d28548e7418f98f385edba1ef667cf7508d1e82
  41. Author: JiuWuyou <JiuWuyou@Abc.com>
  42. Date:   Fri Oct 20 14:34:25 2023 +0800
  43.     add 1
  44. commit 9d6189ffbbf3da34e33d24b5058a155f3d1f5bda
  45. Author: JiuWuyou <JiuWuyou@Abc.com>
  46. Date:   Fri Oct 20 14:33:51 2023 +0800
  47.     add base
复制代码
pick下令演示-更改提交次序,删除提交

   pick保留使用该commit。重新安排pick下令的次序会改变提交的次序。如果选择不包罗提交,则应该删除整行。
  假设我们如今要改变已提交的5.txt和4.txt的次序,该怎样操作?
第一步:告诉git我要操作最近的2次提交
  1. git rebase -i HEAD~2
复制代码
接着,git交互式窗口弹出等待进一步操作,如下是上面下令实行后的弹框
  1. pick 8117d20 add 4
  2. pick 24b9681 add 5
  3. # Rebase 4e81533..24b9681 onto 4e81533 (2 commands)
  4. #
  5. # Commands:
  6. # p, pick <commit> = use commit
  7. # r, reword <commit> = use commit, but edit the commit message
  8. # e, edit <commit> = use commit, but stop for amending
  9. # s, squash <commit> = use commit, but meld into previous commit
  10. # f, fixup <commit> = like "squash", but discard this commit's log message
  11. # x, exec <command> = run command (the rest of the line) using shell
  12. # b, break = stop here (continue rebase later with 'git rebase --continue')
  13. # d, drop <commit> = remove commit
  14. # l, label <label> = label current HEAD with a name
  15. # t, reset <label> = reset HEAD to a label
  16. # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
  17. # .       create a merge commit using the original merge commit's
  18. # .       message (or the oneline, if no original merge commit was
  19. # .       specified). Use -c <commit> to reword the commit message.
  20. #
  21. # These lines can be re-ordered; they are executed from top to bottom.
  22. #
  23. # If you remove a line here THAT COMMIT WILL BE LOST.
  24. #
  25. # However, if you remove everything, the rebase will be aborted.
  26. #
复制代码
第二步:把第一行和第二行互换次序,即如下次序,然后在vim编辑器中保存
  1. pick 24b9681 add 5
  2. pick 8117d20 add 4
复制代码
然后使用git log检察内容如下,乐成
  1. $ git log
  2. commit 360558534b8b79dafdd77131485b252a0ad3bdd6 (HEAD -> master)
  3. Author: JiuWuyou <JiuWuyou@Abc.com>
  4. Date:   Fri Oct 20 14:35:42 2023 +0800
  5.     add 4
  6. commit 40fecbc61d8d318d23b856e5e075600667dc9fdc
  7. Author: JiuWuyou <JiuWuyou@Abc.com>
  8. Date:   Fri Oct 20 14:36:13 2023 +0800
  9.     add 5
  10. commit 4e8153308b8d71e89eddb6759881c4dd8838d2d9
  11. Author: JiuWuyou <JiuWuyou@Abc.com>
  12. Date:   Fri Oct 20 14:35:09 2023 +0800
  13.     add 3
复制代码
假设我们紧接着要删除add 5这次提交,该怎样做?
我们只必要在弹出的交互式窗口中删除如下这一行,保存退出即可
  1. pick 24b9681 add 5
复制代码
使用git log检察结果如下,add 5的提交被删除,乐成
  1. $ git log
  2. commit 958d1bc05005f0141815e76f498406f443912e8d (HEAD -> master)
  3. Author: JiuWuyou <JiuWuyou@Abc.com>
  4. Date:   Fri Oct 20 14:35:42 2023 +0800
  5.     add 4
  6. commit 4e8153308b8d71e89eddb6759881c4dd8838d2d9
  7. Author: JiuWuyou <JiuWuyou@Abc.com>
  8. Date:   Fri Oct 20 14:35:09 2023 +0800
  9.     add 3
  10. commit ba2d4a8dd4976e63903f8e7777dbead108c5dbcb
  11. Author: JiuWuyou <JiuWuyou@Abc.com>
  12. Date:   Fri Oct 20 14:34:50 2023 +0800
  13.     add 2
  14. commit 7d28548e7418f98f385edba1ef667cf7508d1e82
  15. Author: JiuWuyou <JiuWuyou@Abc.com>
  16. Date:   Fri Oct 20 14:34:25 2023 +0800
  17.     add 1
  18. commit 9d6189ffbbf3da34e33d24b5058a155f3d1f5bda
  19. Author: JiuWuyou <JiuWuyou@Abc.com>
  20. Date:   Fri Oct 20 14:33:51 2023 +0800
  21.     add base
复制代码
record使用该commit并编辑(提交内容稳定)

   record雷同于pick,但是在使用它之后,rebase进程将停息,并给您一个修改提交消息的时机。提交所做的任何更改都不受影响
  假设我们要修改add 2的提交comment信息,该怎么操作?
使用git log检察add 2的提交属于最近提交的倒数第3次,则我们要使用下面的下令
  1. git rebase -i HEAD~3
复制代码
弹出如下信息:
  1. pick ba2d4a8 add 2
  2. pick 4e81533 add 3
  3. pick 958d1bc add 4
  4. # Rebase 7d28548..958d1bc onto 7d28548 (3 commands)
  5. #
复制代码
我们只必要修改第一行的add 2如下:(r ba2d4a8 add 2或record ba2d4a8 add 2都可以),然后保存退出
  1. r ba2d4a8 add 2
  2. pick 4e81533 add 3
  3. pick 958d1bc add 4
  4. # Rebase 7d28548..958d1bc onto 7d28548 (3 commands)
  5. #
复制代码
紧接着弹出如下交互界面
  1. add 2
  2. # Please enter the commit message for your changes. Lines starting
  3. # with '#' will be ignored, and an empty message aborts the commit.
  4. #
  5. # Date:      Fri Oct 20 14:34:50 2023 +0800
  6. #
  7. # interactive rebase in progress; onto 7d28548
  8. # Last command done (1 command done):
  9. #    reword ba2d4a8 add 2
  10. # Next commands to do (2 remaining commands):
  11. #    pick 4e81533 add 3
  12. #    pick 958d1bc add 4
  13. # You are currently editing a commit while rebasing branch 'master' on '7d28548'.
  14. #
  15. # Changes to be committed:
  16. #       new file:   2.txt
复制代码
我们如今就可以修改add 2这个提交信息了,我们将第一行add 2修改如下
  1. add 2 - new commit
复制代码
保存退出后,使用git log检察发现add 2的提交信息乐成修改了
  1. 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
  2. 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两次提交之间再加提交,该怎样操作?
我们先输入如下下令
  1. git rebase -i HEAD~2
复制代码
弹出如下信息
  1. pick 9a19efc add 3
  2. pick bec01eb add 4
  3. # Rebase 7c628db..bec01eb onto 7c628db (2 commands)
复制代码
修改如下
  1. e 9a19efc add 3
  2. pick bec01eb add 4
  3. # Rebase 7c628db..bec01eb onto 7c628db (2 commands)
复制代码
保存退出后,git反馈如下信息
  1. $ git rebase -i HEAD~2
  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文本中增长一些内容,然后提交
  1. JiuWuyou@ABC MINGW64 /d/Code/gitopr (master|REBASE 1/2)
  2. $ git add 3.txt
  3. JiuWuyou@ABC MINGW64 /d/Code/gitopr (master|REBASE 1/2)
  4. $ git commit -m"edit 3.txt"
  5. [detached HEAD cfb4f5b] edit 3.txt
  6. 1 file changed, 1 insertion(+)
复制代码
紧接着我们继承rebase
  1. JiuWuyou@ABC MINGW64 /d/Code/gitopr (master|REBASE 1/2)
  2. $ git rebase --continue
  3. Successfully rebased and updated refs/heads/master.
  4. JiuWuyou@ABC MINGW64 /d/Code/gitopr (master)
复制代码
根据上述信息,rebase乐成,然后我们使用git log检察,结果如下,乐成在add 3和add 4之间提交了一次新的提交
  1. $ git log
  2. commit c0b72762408e0d28a914dcae98ef5c41ff6ff662 (HEAD -> master)
  3. Author: JiuWuyou <JiuWuyou@Abc.com>
  4. Date:   Fri Oct 20 14:35:42 2023 +0800
  5.     add 4
  6. commit cfb4f5b8ab1fb6794c0e219366bd9b4eb625d91f
  7. Author: JiuWuyou <JiuWuyou@Abc.com>
  8. Date:   Fri Oct 20 15:06:27 2023 +0800
  9.     edit 3.txt
  10. commit 9a19efcb04106fbb029f84d7e5e36f8fabe5a763
  11. Author: JiuWuyou <JiuWuyou@Abc.com>
  12. Date:   Fri Oct 20 14:35:09 2023 +0800
  13.     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即可
  1. 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)
  2. $ git rebase --continue
  3. Successfully rebased and updated refs/heads/master.
  4. JiuWuyou@ABC MINGW64 /d/Code/gitopr (master)
  5. $ 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给用户时机编写描述这两个更改的新提交消息。
  1. $ git log
  2. commit 4bdef431d96b06ccf128cc71647f77bfffc7bc9e (HEAD -> master)
  3. Author: JiuWuyou <JiuWuyou@Abc.com>
  4. Date:   Fri Oct 20 14:35:42 2023 +0800
  5.     add 4
  6. commit c1afb8db039a9ca3f19862561fb282682ff51095
  7. Author: JiuWuyou <JiuWuyou@Abc.com>
  8. Date:   Fri Oct 20 15:06:27 2023 +0800
  9.     edit 3.txt
  10. commit 9a19efcb04106fbb029f84d7e5e36f8fabe5a763
  11. Author: JiuWuyou <JiuWuyou@Abc.com>
  12. Date:   Fri Oct 20 14:35:09 2023 +0800
  13.     add 3
复制代码
假设我们要将add 4和edit 3.txt这两次提交合并,我们该怎么操作?
  1. git rebase -i HEAD~2
复制代码
再弹出的交互窗口中将add 4前的pick修改为s,如下
  1. pick c1afb8d edit 3.txt
  2. s 4bdef43 add 4
复制代码
保存退出后,git弹出窗口答应我们修改提交信息,默认是两个提交消息合并,我们可以啥都不改,保存退出
  1. # This is a combination of 2 commits.
  2. # This is the 1st commit message:
  3. edit 3.txt
  4. # This is the commit message #2:
  5. add 4
  6. # Please enter the commit message for your changes. Lines starting
  7. # with '#' will be ignored, and an empty message aborts the commit.
  8. #
  9. # Date:      Fri Oct 20 15:06:27 2023 +0800
  10. #
  11. # interactive rebase in progress; onto 9a19efc
  12. # Last commands done (2 commands done):
  13. #    pick c1afb8d edit 3.txt
  14. #    squash 4bdef43 add 4
  15. # No commands remaining.
  16. # You are currently rebasing branch 'master' on '9a19efc'.
  17. #
  18. # Changes to be committed:
  19. #       modified:   3.txt
  20. #       new file:   4.txt
复制代码
使用git log查询如下,合并乐成
  1. $ git log
  2. commit 60b3ebdd3cb11fbcba3aaa33911c13881532dce1 (HEAD -> master)
  3. Author: JiuWuyou <JiuWuyou@Abc.com>
  4. Date:   Fri Oct 20 15:06:27 2023 +0800
  5.     edit 3.txt
  6.     add 4
  7. commit 9a19efcb04106fbb029f84d7e5e36f8fabe5a763
  8. Author: JiuWuyou <JiuWuyou@Abc.com>
  9. Date:   Fri Oct 20 14:35:09 2023 +0800
  10.     add 3
复制代码
fixup将该commit合并到前一个commit,不必要保留该commit的注释

   与squash雷同,但是要合并的提交会丢弃其消息。提交简朴地合并到它上面的提交中,并且早先的提交的消息用于描述这两个更改。
  假设我们要将edit 3.txt的提交内容与add 3合并,并丢弃edit 3.txt的提交信息,该怎样操作?
  1. git rebase -i HEAD~2
复制代码
弹出如下信息
  1. pick 9a19efc add 3
  2. pick 60b3ebd edit 3.txt
  3. # Rebase 7c628db..60b3ebd onto 7c628db (2 commands)
复制代码
更改为
  1. pick 9a19efc add 3
  2. f 60b3ebd edit 3.txt
  3. # Rebase 7c628db..60b3ebd onto 7c628db (2 commands)
复制代码
保存退出后,使用git log检察发现edit 3.txt的提交信息消失,但edit 3.txt文本中的提交内容仍然保留着
  1. $ git log
  2. commit e43925beedb4520924d9996e3356040087531c3a (HEAD -> master)
  3. Author: JiuWuyou <JiuWuyou@Abc.com>
  4. Date:   Fri Oct 20 14:35:09 2023 +0800
  5.     add 3
  6. commit 7c628dbc275b749e8a6d20cff13f33d55325dc07
  7. Author: JiuWuyou <JiuWuyou@Abc.com>
  8. Date:   Fri Oct 20 14:34:50 2023 +0800
  9.     add 2 - new commit
复制代码
exec 实行shell下令

  1. git rebase -i HEAD~3
复制代码
  1. pick 7d28548 add 1pick 7c628db add 2 - new commit
  2. pick e43925b add 3# Rebase 9d6189f..e43925b onto 9d6189f (3 commands)
复制代码
我们在上面的弹框内容前加一行shell下令
  1. x echo "execute print command....."pick 7d28548 add 1pick 7c628db add 2 - new commit
  2. pick e43925b add 3# Rebase 9d6189f..e43925b onto 9d6189f (3 commands)
复制代码
保存退出,shell会打印“execute print command…”内容
  1. $ git rebase -i HEAD~3
  2. 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的操作要多多练习,练习中不要怕出错  
   
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4