git rebase在《git權威指南》一書中被翻譯為變基,聽着有些別扭吧,變基變基,變成庫克了,在《pro git》中被翻譯成衍合,所以以后git rebase均使用《pro git》中的翻譯方式。
在git中將個分支中的修改整合到另一個分支的辦法有兩種:merge和rebase,現在又如下使用情景,在master分支的第3次提交產生一個分支dev,在這個dev分支上做了兩次提交,而此時master分支由於某些原因又進行了兩次提交,現在需要將dev分支和master分支合並,這時有兩種方法實現,一種是merge一種是rebase。
使用merge時在dev分支中的提交不會發生變化,這對於有其他人使用次分支時很重要,但是這個操作會產生三個提交,包含一個合並提交,因此在將特性分支合並到主干master上時一般不使用merge;使用rebase操作看起來就像是順序提交,方便管理,rebase操作的具體過程為:
- 確保工作區位於dev分支上:git checkout dev
- 執行衍合操作:git rebase master
- 如果有沖突需要先解決沖突,解決完沖突之后執行:git rebase --continue
- 如果想放棄這次操作可以執行:git rebase --abort
- 如果是想直接使用master分支取代此分支,可以執行:git rebase --skip
可以給rebase加上-i參數進行交互式rebase,
交互式rebase提供了一個簡單易用的途徑讓你在和別人 分享提交之前對你的提交進行分割、合並或者重排序。在把從其他開發者處拉取的提交應用到本地時,你也可以使用交互式rebase對它們進行清理。如果你想在rebase的過程中對一部分提交進行修改,你可以在'git rebase'命令中加入'-i'或'--interactive'參數去調用交互模式。
git commit -i commit號
使用-i進行交互式rebase時會有一些交互式命令
# 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
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
# 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
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
例如可以使用pick命令改變提交的順序,如果不會有沖突;使用squash命令,git會把這個提交和前一個提交合並成為一個新的提交,這會再次調用編輯器,可以在里面合並這兩個提交的提交信息;如果指定進行'edit'操作,git會完成同樣的工作,但是在對下一提交進行操作之前,它會返回到命令行讓你對提交進行修正,或者對提交內容進行修改;交互式rebase的另外一個作用是丟棄提交,如果把一行刪除而不是指定'pick'、'squash'和‘edit''中的任何一個,git會從歷史中移除該提交。
參考資料:
- http://book.douban.com/subject/3420144/
- http://book.douban.com/subject/6526452/
- http://blog.chinaunix.net/uid-27714502-id-3436706.html
- http://blog.csdn.net/lihaoweiv/article/details/7740031