git 合併多個commit

當你提交代碼進行代碼審查時或者創建一次pull request (這在開源項目中經常發生),你的代碼在被接受之前會被要求做一些變更。於是你進行了變更,並且直到下一次審查之前你沒有再次被要求進行變更過。在你知道又要進行變更之前,你已經有了一些額外的commit。理想情況下,你可以用rebase命令把多個commit壓縮成一個。

git rebase -i HEAD~[number_of_commits]

如果你想要壓縮最後兩個commit,你需要運行下列命令。

git rebase -i HEAD~3

爲了模擬實際git rebase效果,我們先在git上提交兩個修改。git log如下:

commit 7b16b280f22fe4ff57c1879867a624f6f0f14398Author: pan Date:   Sun Apr 22 08:55:32 2018 +0800    update3
commit a7186d862b95efc5cc1d7c98277af4c72bac335dAuthor: pan Date:   Sun Apr 22 08:55:16 2018 +0800    update2 
commit 16a9a4749f8ee25ab617c46925f57c2fa8a4937eAuthor: pan Date:   Sun Apr 22 08:54:55 2018 +0800    update1

假設合併這3個提交,可以按照下面過程

git rebase -i HEAD~3

執行命令後終端會出輸出:

pick 16a9a47 update3 
pick a7186d8 update2
pick 7b16b28 update1
 # Rebase a9269a3..7b16b28 onto a9269a3 (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~                                                   

注:

第一列是rebase具體執行的操作,其中操作可以選擇,其中含義如下:

選擇pick操作,git會應用這個補丁,以同樣的提交信息(commit message)保存提交

選擇reword操作,git會應用這個補丁,但需要重新編輯提交信息

選擇edit操作,git會應用這個補丁,但會因爲amending而終止

選擇squash操作,git會應用這個補丁,但會與之前的提交合並

選擇fixup操作,git會應用這個補丁,但會丟掉提交日誌

選擇exec操作,git會在shell中運行這個命令


對比之前的兩個提交提交,我覺得第一個提交可以保留,第二個和第三個合併到第一個就可以了。

將第二個和第三個pick改成squash或者s,然後保存退出。如下:

pick 16a9a47 update3 
s a7186d8 update2
s 7b16b28 update1

此時git會自動將第二個提交合併到第一個提交,並彈出合併提示信息,如下:

# This is a combination of 3 commits.
# This is the 1st commit message:
update3 
 # This is the commit message #2:
 update2 
 # This is the commit message #3:
 update1
 # Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sun Apr 22 08:54:55 2018 +0800
#
# interactive rebase in progress; onto a9269a3
# Last commands done (3 commands done):
#    s a7186d8 update
#    s 7b16b28 update6666
#  No commands remaining.

如果需要修改下提交信息,如果不需要直接保存退出即可。

# This is a combination of 3 commits. 

合併提交測試

# Please enter the commit message for your changes. Lines starting 

# with '#' will be ignored, and an empty message aborts the commit. 

# Date:      Sun Apr 22 08:54:55 2018 +0800 

# interactive rebase in progress; onto a9269a3 

# Last commands done (3 commands done): 

#    s a7186d8 update 

#    s 7b16b28 update6666 

#  No commands remaining.

此時我們已經完成了將兩個提交合併爲一個的處理,可以通過git log查看

commit 4a51759fae9bbd84904029473fe09f8a77f143ed
Author: pan 
Date:   Sun Apr 22 08:54:55 2018 +0800    合併提交測試

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章