標籤:
使用rebase -i會在終端出現一個互動頁面. 在這個互動頁面中我們可以對要rebase的commit做一定的修改.
用法
git rebase -i <master>
把當前的分支的commit放在<base>後面, -i會開啟一個編輯器, 在這你可以為每一個commit輸入一個命令, 這個命令決定了如何把單個的commit傳輸到new base. 還可以改變commit列表的順序.
討論
大多數開發人員喜歡在merge一個分支到master的時候使用rebase -i打磨我們這個feature分支. 他給了我們機會合并一些commit, 刪除一些commit, 對commit進行排序.
例子
# Start a new featuregit checkout -b new-feature master# Edit filesgit commit -a -m "Start developing a feature"# Edit more filesgit commit -a -m "Fix something from the previous commit"# Add a commit directly to mastergit checkout master# Edit filesgit commit -a -m "Fix security hole"# Begin an interactive rebasing sessiongit checkout new-featuregit rebase -i master
最後的命令會開啟一個編輯器, 其中有兩行命令.
pick 32618c4 Start developing a featurepick 62eed47 Fix something from the previous commit
在這你可以修改pick命令. 在這個例子中, 我們使用squash合并這兩個commit:
pick 32618c4 Start developing a featuresquash 62eed47 Fix something from the previous commit
儲存關閉編輯器後rebase開始. 這是會開啟一個新的編輯器讓我們填寫commit描述.整個過程可視化如下:
注意了squash commit有一個新的ID號, 這表明他是一個全新的commit.
最後使用fase -forward merge整合feature分支:
git checkout mastergit merge new-feature
git rebase -i