git rebase -i 提供一個(gè)參數(shù),指明你想要修改的提交的父提交(-i 是--interactive的縮寫(xiě))
例如:修改最近三次提交,事實(shí)上所指的是四次提交之前,即你想修改的提交的父提交
$ git rebase -i HEAD~3
執(zhí)行g(shù)it 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
其實(shí)整個(gè)編輯框就是一個(gè)腳本,它會(huì)自頂向下執(zhí)行每一條命令。命令執(zhí)行的順序可以改變,也可以刪除某條命令,對(duì)應(yīng)的commit就移除了。
修改多個(gè)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
當(dāng)執(zhí)行到這條命令的時(shí)候它會(huì)停留在你想修改的變更上
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
修改完成后退出編輯器。然后,運(yùn)行
$ git rebase --continue
修改提交的commit的順序
更改行的順序即可改變commit的提交順序,只適用于commit之間沒(méi)有依賴(lài)關(guān)系的情況
合并提交
只需要將pick改為squash或者fixup,squash和fixup的唯一區(qū)別是一個(gè)保留commit信息,一個(gè)忽略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
拆分提交
拆分提交就是撤銷(xiāo)一次提交,然后再多次部分地暫存和提交
將你想拆分的提交前的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
通過(guò)git reset HEAD^ 撤銷(xiāo)那次提交并將修改的文件撤回
$ git reset HEAD^
然后再多次部分地暫存和提交
$ git add README.md
$ git commit -m 'Update README.md'
$ git add Api/ProxyApi.py
$ git commit -m 'update host'
最后運(yùn)行 git rebase --continue
$ git rebase --continue