git rebase -i 提供一個參數,指明你想要修改的提交的父提交(-i 是--interactive的縮寫)
例如:修改最近三次提交,事實上所指的是四次提交之前,即你想修改的提交的父提交
$ git rebase -i HEAD~3
執行git rebase -i HEAD~3命令,彈出如下編輯框:
pick c137cb8 Update README.md
pick e357b54 update host
pick 63936af add circle ci, appveyor ci for integration testing and use codecov to do the coverage test
# Rebase 20e39bf..63936af onto 20e39bf (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# 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.
#
# Note that empty commits are commented out
其實整個編輯框就是一個腳本,它會自頂向下執行每一條命令。命令執行的順序可以改變,也可以刪除某條命令,對應的commit就移除了。
修改多個commit message
將你想修改的每一次提交前面的pick改為edit,保存并退出編輯器
pick c137cb8 Update README.md
edit e357b54 update host
pick 63936af add circle ci, appveyor ci for integration testing and use codecov to do the coverage test
當執行到這條命令的時候它會停留在你想修改的變更上
warning: Stopped at e357b54... update host
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
你可以輸入git commit --amend 修改 commit message
$ git commit --amend
修改完成后退出編輯器。然后,運行
$ git rebase --continue
修改提交的commit的順序
更改行的順序即可改變commit的提交順序,只適用于commit之間沒有依賴關系的情況
合并提交
只需要將pick改為squash或者fixup,squash和fixup的唯一區別是一個保留commit信息,一個忽略commit信息
pick c137cb8 Update README.md
fixup e357b54 update host
pick 63936af add circle ci, appveyor ci for integration testing and use codecov to do the coverage test
合并后的log信息如下:
pick f371b8c Update README.md
pick 9f215a6 add circle ci, appveyor ci for integration testing and use codecov to do the coverage test
拆分提交
拆分提交就是撤銷一次提交,然后再多次部分地暫存和提交
將你想拆分的提交前的pick改為edit
pick c137cb8 Update README.md
edit e357b54 update host
pick 63936af add circle ci, appveyor ci for integration testing and use codecov to do the coverage test
通過git reset HEAD^ 撤銷那次提交并將修改的文件撤回
$ git reset HEAD^
然后再多次部分地暫存和提交
$ git add README.md
$ git commit -m 'Update README.md'
$ git add Api/ProxyApi.py
$ git commit -m 'update host'
最后運行 git rebase --continue
$ git rebase --continue