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