1,查看提交歷史,git log
首先你要知道自己想合並的是哪幾個提交,可以使用git log命令來查看提交歷史,假如最近4條歷史如下:
commit 3ca6ec340edc66df13423f36f52919dfa3......
commit 1b4056686d1b494a5c86757f9eaed844......
commit 53f244ac8730d33b353bee3b24210b07......
commit 3a4226b4a0b6fa68783b07f1cee7b688.......
歷史記錄是按照時間排序的,時間近的排在前面。
2,git rebase
想要合並1-3條,有兩個方法
1.從HEAD版本開始往過去數3個版本
git rebase -i HEAD~3
2.指名要合並的版本之前的版本號
git rebase -i 3a4226b
請注意3a4226b這個版本是不參與合並的,可以把它當做一個坐標
3,選取要合並的提交
1.執行了rebase命令之后,會彈出一個窗口,頭幾行如下:
pick 3ca6ec3 '注釋**********'
pick 1b40566 '注釋*********'
pick 53f244a '注釋**********'
2.將pick改為squash或者s,之后保存並關閉文本編輯窗口即可。改完之后文本內容如下:
s 3ca6ec3 '注釋**********'
s 1b40566 '注釋*********'
s 53f244a '注釋**********'
3.ctrl+C退編輯狀態,再輸入:wq,保存退出,Git會壓縮提交歷史,如果有沖突,需要修改,修改的時候要注意,保留最新的歷史,不然我們的修改就丟棄了。修改以后要記得敲下面的命令:
git add .
git rebase --continue
如果你想放棄這次壓縮的話,執行以下命令:
git rebase --abort
4.如果沒有沖突,或者沖突已經解決,則會出現如下的編輯窗口:
# This is a combination of 4 commits.
#The first commit’s message is:
注釋......
# The 2nd commit’s message is:
注釋......
# The 3rd commit’s message is:
注釋......
# Please enter the commit message for your changes. Lines starting # with ‘#’ will be ignored, and an empty message aborts the commit.
5.輸入wq保存並推出, 再次輸入git log查看 commit 歷史信息,你會發現這兩個 commit 已經合並了。