ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 以压缩最近的三个提交为一个提交为例 * 最好在压缩提交前创建 backup 分支,以备不时之需 * 如需练习,请查看[练习题](https://github.com/yangmin4052/my-git-practice)压缩习题一 ```bash git branch backup topicB ``` ```bash # 变基前 ---a---b---c master \ d---e---f topicA \ g---h---i HEAD -> topicB, backup ``` ### 第一步 * 执行以下命令 ```bash git rebase -i HEAD~3 ``` ### 第二步 #### 1. 弹出第一个界面 ```bash pick 3360d12 <第一次提交信息> pick bc293e2 <第二次提交信息> pick 1726edf <第三次提交信息> # Rebase d65c58e..1726edf onto d65c58e (3 commands) # # Commands: # p, pick = use commit 正常选中 # r, reword = use commit, but edit the commit message 选中,并且修改提交信息; # e, edit = use commit, but stop for amending 选中,rebase时会暂停,允许你修改这个commit(参考这里) # s, squash = use commit, but meld into previous commit 选中,会将当前commit与上一个commit合并 # f, fixup = like "squash", but discard this commit's log message 与squash相同,但不会保存当前commit的提交信息 # x, exec = run command (the rest of the line) using shell 执行其他shell命令 # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # ``` #### 2. 使用编辑器将其改为以下内容并保存退出 ```bash r 3360d12 <第一次提交信息> s bc293e2 <第二次提交信息> s 1726edf <第三次提交信息> # Rebase d65c58e..1726edf onto d65c58e (3 commands) # # Commands: # p, pick = use commit 正常选中 # r, reword = use commit, but edit the commit message 选中,并且修改提交信息; # e, edit = use commit, but stop for amending 选中,rebase时会暂停,允许你修改这个commit(参考这里) # s, squash = use commit, but meld into previous commit 选中,会将当前commit与上一个commit合并 # f, fixup = like "squash", but discard this commit's log message 与squash相同,但不会保存当前commit的提交信息 # x, exec = run command (the rest of the line) using shell 执行其他shell命令 # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # ``` ### 第三步 #### 1. 弹出第二个界面 ```bash <第一次提交信息> # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Thu May 3 19:59:00 2018 +0800 # # interactive rebase in progress; onto d65c58e # Last command done (1 command done): # r 3360d12 g # Next commands to do (2 remaining commands): # s bc293e2 h # s 1726edf i # You are currently editing a commit while rebasing branch 'topicB' on 'd65c58e'. # # Changes to be committed: # new file: g# ``` #### 2. 使用编辑器将其改为以下内容并保存退出 ```bash <新的提交信息> # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Thu May 3 19:59:00 2018 +0800 # # interactive rebase in progress; onto d65c58e # Last command done (1 command done): # r 3360d12 g # Next commands to do (2 remaining commands): # s bc293e2 h # s 1726edf i # You are currently editing a commit while rebasing branch 'topicB' on 'd65c58e'. # # Changes to be committed: # new file: g# ``` ### 第四步 #### 1. 弹出第三个界面 ```bash # This is a combination of 3 commits. # This is the 1st commit message: <新的提交信息> # This is the commit message #2: <第二次提交信息> # This is the commit message #3: <第三次提交信息> # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Thu May 3 19:59:00 2018 +0800 ## interactive rebase in progress; onto d65c58e# Last commands done (3 commands done):# s bc293e2 h# s 1726edf i# No commands remaining. # You are currently rebasing branch 'topicB' on 'd65c58e'.## Changes to be committed:# new file: g# new file: h# new file: i # ``` #### 2. 使用编辑器将其改为以下内容并保存退出 ```bash # This is a combination of 3 commits. <新的提交信息> # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Thu May 3 19:59:00 2018 +0800 ## interactive rebase in progress; onto d65c58e# Last commands done (3 commands done):# s bc293e2 h# s 1726edf i# No commands remaining. # You are currently rebasing branch 'topicB' on 'd65c58e'.## Changes to be committed:# new file: g# new file: h# new file: i # ``` ### 第五步 * 到此,压缩提交成功,可以查看一下 ```bash # 变基前 ---a---b---c master \ d---e---f topicA \ g---h---i HEAD -> topicB, backup ``` ```bash # 变基后 ---a---b---c master \ d---e---f topicA |\ | g---h---i backup \ ghi HEAD -> topicB ```