1. 查看commit
$ git log
commit 12345678
Add third commit
commit abcdefgh
Add second commit
commit 88888888
Add first commit
此時,git log
有三個HEAD~3 commits : 12345678, abcdefgh, 8888888. 現在,我們需要合併前兩個commits.
2. 合併commit
- 合併HEAD版本前兩個commits(包含HEAD):
git rebase -i HEAD~2
- 合併指定版本之前的commits:
$ git rebase -i 88888888
注意,-i
的commit並不包含在合併的範圍內.
3. 編輯commit
運行上述命令,會進入一個編輯器:
pick 123456 Add second commit
pick abcdef Add third commit
修改爲:
pick 123456 Add second commit
squash abcdef Add third commit
- pick: 選擇保留這個commit
- squash: 合併該commit到前一個pick的commit
然後,進入下一個編輯器:
# This is a combination of 2 commits.
# The first commit's message is:
Add second commit
# This is the 2nd commit message:
Add third commit
# please enter the commit message for your change...
將它們全刪掉,重新修改自己的commit:
Add combinate commits
# please enter the commit message for your change...
保存退出,檢查commits:
$ git log
commit 99999999
Add combinate commits
commit 88888888
Add first commit
注意,如果操作錯誤或還原commits,請運行git rebase --abort
進行撤銷.