当前位置:   article > 正文

Git(10)git修改历史提交_git rebase -i 交换提交顺序

git rebase -i 交换提交顺序
git rebase -i <command>

<command>有六个:

1、pick

        pick只是意味着包括提交。重新进行命令时,不做任何操作则不改变提交的任何内容及信息;若新安排pick命令之间的顺序则会更改提交的顺序;若选择不包括提交,则应删除整行。

2、reword

        该reword命令与pick相似,使用后将暂停重新设置功能,并为您提供修改提交消息的机会,提交所包括的任何更改均不受影响。

3、edit

        如果您选择edit提交,则将有机会修改提交内容,这意味着可以完全添加或更改提交内容。您还可以进行更多提交,然后再继续进行变基。这使您可以将大型提交拆分为较小的提交,或者删除在提交中所做的错误更改。

4、squash

        该命令使您可以将两个或多个提交合并为一个提交。提交被压缩到其上方的提交中。Git使您有机会编写描述这两个更改的新提交消息。

5、fixup

        这类似于squash,但是要合并的提交已丢弃其消息。提交仅合并到其上方的提交中,并且较早提交的消息用于描述这两个更改。

6、exec

        这使您可以对提交运行任意的Shell命令

前置工作

  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 a75ed742838ebc1ef1073502623478f73e1ec21f
  25. Author:
  26. Date: Wed Mar 4 10:02:51 2020 +0800
  27. add 5
  28. commit 8b485bb4768b2abf8f6400dcba069f1a650ed5ec
  29. Author:
  30. Date: Wed Mar 4 09:59:27 2020 +0800
  31. add 4
  32. commit 63ce9fb010da550c668aca66758c45fbfad46e2b
  33. Author:
  34. Date: Wed Mar 4 09:59:04 2020 +0800
  35. add 3
  36. commit 9cd34c4d42f52cfb40026dae613c8ad29d7cbc66
  37. Author:
  38. Date: Wed Mar 4 09:58:45 2020 +0800
  39. add 2
  40. commit 77bd0eb1a97e1676367ea236c1c47c155eaa8430
  41. Author:
  42. Date: Wed Mar 4 09:58:23 2020 +0800
  43. add 1

一、使用pick更改提交顺序、删除提交

1、改变提交 5.txt 和 4.txt 的顺序,该怎么操作

        更改涉及到了两次提交,最早提交次数为2(4.txt是倒数第二次提交),改变倒数第2次后的提交

(1)执行命令

git rebase -i HEAD~2

接着,git给你一个文本,告诉你我知道了,你说的这些可以有以下操作下面是执行命令后的样子

  1. pick 8b485bb add 4
  2. pick a75ed74 add 5
  3. # Rebase 63ce9fb..a75ed74 onto 63ce9fb (2 command(s))
  4. #
  5. # Commands:
  6. # p, pick = use commit
  7. # r, reword = use commit, but edit the commit message
  8. # e, edit = use commit, but stop for amending
  9. # s, squash = use commit, but meld into previous commit
  10. # f, fixup = like "squash", but discard this commit's log message
  11. # x, exec = run command (the rest of the line) using shell
  12. # d, drop = remove commit
  13. #
  14. # These lines can be re-ordered; they are executed from top to bottom.
  15. #
  16. # If you remove a line here THAT COMMIT WILL BE LOST.
  17. #
  18. # However, if you remove everything, the rebase will be aborted.
  19. #
  20. # Note that empty commits are commented out
  21. ~
  22. ~
  23. ~

解释:我们刚刚执行的命令中 HEAD~2代表选择离HEAD最近的两条提交,下面注释的是提示,我们不需要管,只要专注前两行就ok

(2)把 第一行 和 第二行 交换顺序,变成下面的样子

  1. pick a75ed74 add 5
  2. pick 8b485bb add 4

(3)接着按Esc后输入:wq保存退出,输入命令git log查看,4和5的顺序改变

(4)git push -f 强制推送改变的结果,远程仓库的提交记录中,4和5的顺序也改变了

2、删除 某一个提交,该怎么操作

我们来删除 add 4 的那条提交

(1)执行命令

git rebase -i HEAD~2

出现如下

  1. pick a75ed74 add 5
  2. pick 8b485bb add 4
  3. # Rebase 575fd8b..bb2a77d onto 575fd8b (1 command(s)) 
  4. #
  5. # Commands:
  6. # p, pick = use commit
  7. # r, reword = use commit, but edit the commit message
  8. # e, edit = use commit, but stop for amending
  9. # s, squash = use commit, but meld into previous commit
  10. # f, fixup = like "squash", but discard this commit's log message
  11. # x, exec = run command (the rest of the line) using shell
  12. # d, drop = remove commit
  13. #
  14. # These lines can be re-ordered; they are executed from top to bottom.
  15. #
  16. # If you remove a line here THAT COMMIT WILL BE LOST.
  17. #
  18. # However, if you remove everything, the rebase will be aborted.
  19. #
  20. # Note that empty commits are commented out
  21. ~
  22. ~
  23. ~

(2)删除第二行内容,只剩下pick a75ed74 add 5

(3)接着按Esc后输入:wq保存退出,输入命令git log查看,只剩下5、3、2、1的提交记录

(4)git push -f 强制推送改变的结果,远程仓库的提交记录中,也只剩下5、3、2、1的提交记录

二、使用reword修改提交信息(内容保持不变)

修改一下 add 2 的提交消息,该怎么操作

(1)git log 查看 add 2 距离 HEAD 有多少的距离,得:3    注:之前删除了4的提交记录

(2)执行命令

git rebase -i HEAD~3

出现如下

  1. pick 9cd34c4 add 2
  2. pick 63ce9fb add 3
  3. pick 575fd8b add 5
  4. # Rebase 77bd0eb..575fd8b onto 77bd0eb (3 command(s))
  5. #
  6. # Commands:
  7. # p, pick = use commit
  8. # r, reword = use commit, but edit the commit message
  9. # e, edit = use commit, but stop for amending
  10. # s, squash = use commit, but meld into previous commit
  11. # f, fixup = like "squash", but discard this commit's log message
  12. # x, exec = run command (the rest of the line) using shell
  13. # d, drop = remove commit
  14. #
  15. # These lines can be re-ordered; they are executed from top to bottom.
  16. #
  17. # If you remove a line here THAT COMMIT WILL BE LOST.
  18. #
  19. # However, if you remove everything, the rebase will be aborted.
  20. #
  21. # Note that empty commits are commented out
  22. ~
  23. ~
  24. ~

(3)只需要修改 第一行 add 2 ,其余保持不变

  1. reword 9cd34c4 add 2
  2. pick 63ce9fb add 3
  3. pick 575fd8b add 5
  4. # Rebase 77bd0eb..575fd8b onto 77bd0eb (3 command(s))
  5. #
  6. # Commands:
  7. # p, pick = use commit
  8. # r, reword = use commit, but edit the commit message
  9. # e, edit = use commit, but stop for amending
  10. # s, squash = use commit, but meld into previous commit
  11. # f, fixup = like "squash", but discard this commit's log message
  12. # x, exec = run command (the rest of the line) using shell
  13. # d, drop = remove commit
  14. #
  15. # These lines can be re-ordered; they are executed from top to bottom.
  16. #
  17. # If you remove a line here THAT COMMIT WILL BE LOST.
  18. #
  19. # However, if you remove everything, the rebase will be aborted.
  20. #
  21. # Note that empty commits are commented out
  22. ~
  23. ~
  24. ~

(4)接着按Esc后输入:wq保存退出,会弹出一个编辑窗口

  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:      Wed Mar 4 09:58:45 2020 +0800
  6. #
  7. # interactive rebase in progress; onto 77bd0eb
  8. # Last command done (1 command done):
  9. #    r 9cd34c4 add 2
  10. # Next commands to do (2 remaining commands):
  11. #    pick 63ce9fb add 3
  12. #    pick 575fd8b add 5
  13. # You are currently editing a commit while rebasing branch 'master' on '77bd0eb'.
  14. #
  15. # Changes to be committed:
  16. #       new file:   2.txt
  17. #

(5)编辑窗口中修改提交信息为    add 2 ~ new comment

(6)接着按Esc后输入:wq保存退出,输入命令git log查看,消息 “add 2” 变为了 “add 2 ~ new comment”

注意:这里只修改了2的提交信息,其他提交没有任何的变动

三、edit修改提交

在 add 3 和 add 5 之间 添加一条提交

(1)执行命令

git rebase -i HEAD~2

出现如下: 

  1. pick 6934312 add 3
  2. pick 5ce6dde add 5
  3. # Rebase 7f9d45d..5ce6dde onto 7f9d45d (2 command(s))
  4. # ....

将pick 6934312 add 3修改为e 6934312 add 3

(2)接着 Esc,:wq 保存退出

  1. $ git rebase -i HEAD~2
  2. Stopped at 6934312135c150bf74bead26e371df1443273ca4... add 3
  3. You can amend the commit now, with
  4.         git commit --amend
  5. Once you are satisfied with your changes, run
  6.         git rebase --continue
  7. xxxxxx MINGW32 ~/Desktop/git-demo (master|REBASE-i 1/2)

(3)可以看到,我们的master分支多了REBASE-i 1/2,我们尝试做一些修改,给3.txt 增加一些内容,然后提交

  1. git add 3.txt
  2. git commit -m "edit 3.txt"

出现如下 :

  1. [detached HEAD 7262a57] edit 3.txt
  2.  1 file changed, 1 insertion(+)

(4)接着,我们继续 rebase

git rebase --continue

出现如下 :

Successfully rebased and updated refs/heads/master.

(5)输入git log,在 add 5 和 add 3 中间 增加了我们刚刚的修改

文章参考:【git 整理提交】git rebase -i 命令详解_the_power的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/940614
推荐阅读
  

闽ICP备14008679号