【學了就忘】Git操作 — 64.rebase應用:連續多個提交合併成一個提交

(二)連續多個提交合併成一個提交

在工作當中很可能會遇到一種場景,在編碼的過程中,創建了很多的commit,當功能開發完成之後,發現之前有幾個commit似乎可以合併在一起,這幾個commit就是一個完成的功能,沒有必要拆分成幾個commit提交。

也就是整理歷史提交的場景,把歷史提交中的多個連續的commit,合併成一個commit。

注意:這個場景適用於本地,自己開發的過程中,還沒提交打團隊集成分支的時候。(不能用到團隊的共享分支上,git rebase黃金法則。)

1、查看當前分支的日誌情況

我們可以看到下面歷史提交情況,發現最新生成的幾個commit都是修改hello.html文件的內容,那我們可以把這幾個commit,合併成一個commit。

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git log --oneline
dbdeba5 (HEAD -> master) 第6次提交,新增hello.html文件 v5
9a1342a 第5次提交,新增hello.html文件 v4
c90d574 第4次提交,新增hello.html文件 v3
d5fa1d1 第3次提交,新增hello.html文件 v2
5a5352d 第2次提交,新增hello.html文件 v1
69edafd 第1次提交,新增readme.txt文件 v1

2、執行commit合併(重點)

需求:把最近提交4個的commit合併成爲一個commit。

說明:

  • 我們用到的命令,還是git rebase命令。
  • 我們要合併最新提交的4個commit,所以這個變基的基要選擇第五個commit。

(1)執行$ git rebase -i 5a5352d命令

進入到交互頁面中,看看裏邊的內容。

pick d5fa1d1 第3次提交,新增hello.html文件 v2
pick c90d574 第4次提交,新增hello.html文件 v3
pick 9a1342a 第5次提交,新增hello.html文件 v4
pick dbdeba5 第6次提交,新增hello.html文件 v5

# Rebase 5a5352d..dbdeba5 onto dbdeba5 (4 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# 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

說明:在合併的提交中,必須有一個策略爲pick,意思是基於哪個提交合並,或者說這幾個提交合到哪個commit上。

注意:上圖中的四個commit,從上到下,越上commit的提交時間越早。

我們一般選擇第一提交d5fa1d1爲pick,其他的提交都合在這個commit上。

(2)選擇策略

其他被合併的commit,我們要選擇一下他們的策略。用到的策略如下:

s, squash <commit> = use commit, but meld into previous commit

這個策略就是我們要處理的場景。

  • use commit:表示這些提交都要保留。
  • but meld into previous commit:但是要把他合併到前面的commit裏。

(3)編輯rebase交互頁面中的策略

編輯完成,意思是說把策略爲s的提交內容,合併到策略爲pick的提交當中去。

pick d5fa1d1 第3次提交,新增hello.html文件 v2
s c90d574 第4次提交,新增hello.html文件 v3
s 9a1342a 第5次提交,新增hello.html文件 v4
s dbdeba5 第6次提交,新增hello.html文件 v5

保存退出(:wq!)。

(4)編輯rebase第二個交互頁面

上邊保存退出之後,會直接跳轉到這個交互頁面上。

如下:

# This is a combination of 4 commits.
# This is the 1st commit message:

第3次提交,新增hello.html文件 v2

# This is the commit message #2:

第4次提交,新增hello.html文件 v3

# This is the commit message #3:

第5次提交,新增hello.html文件 v4

# This is the commit message #4:

第6次提交,新增hello.html文件 v5

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sat May 1 17:50:22 2021 +0800
#
# interactive rebase in progress; onto 5a5352d
# Last commands done (4 commands done):
#    s 9a1342a 第5次提交,新增hello.html文件 v4
#    s dbdeba5 第6次提交,新增hello.html文件 v5
# No commands remaining.
# You are currently rebasing branch 'master' on '5a5352d'.
#
# Changes to be committed:
#       modified:   hello.html
#

這個交互窗口的意思是,把4個commit進行合併,讓我們填寫爲什麼要做這次變更,爲什麼要把這幾個commit合併成一個提交。

該頁面編輯如下:

# This is a combination of 4 commits.

修改hello.html文件內容

第3次提交,新增hello.html文件 v2

第4次提交,新增hello.html文件 v3

第5次提交,新增hello.html文件 v4

第6次提交,新增hello.html文件 v5

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sat May 1 17:50:22 2021 +0800
#
# interactive rebase in progress; onto 5a5352d
# Last commands done (4 commands done):
#    s 9a1342a 第5次提交,新增hello.html文件 v4
#    s dbdeba5 第6次提交,新增hello.html文件 v5
# No commands remaining.
# You are currently rebasing branch 'master' on '5a5352d'.
#
# Changes to be committed:
#       modified:   hello.html

提示:with '#' will be ignored, and an empty message aborts the commit.的意思是:帶#號的表示備註,在提交的message中是不會出現的。

保存退出(:wq!)。

如果Git執行完rebase操作沒有問題,就會顯示如下信息:

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git rebase -i 5a5352d
[detached HEAD 840f01d] 修改hello.html文件內容
 Date: Sat May 1 17:50:22 2021 +0800
 1 file changed, 4 insertions(+)
Successfully rebased and updated refs/heads/master.

說明:rebase操作已經成功,並且分支頭指針,也指向了最新的commit。

(原理同上面應用,這裏就不重複了)

(5)查看版本庫歷史提交記錄

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git log --oneline
840f01d (HEAD -> master) 修改hello.html文件內容
5a5352d 第2次提交,新增hello.html文件 v1
69edafd 第1次提交,新增readme.txt文件 v1

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git log 
commit 840f01db9f204aff4543345d65b22cb68aee574c (HEAD -> master)
Author: sun_wk <[email protected]>
Date:   Sat May 1 17:50:22 2021 +0800

    修改hello.html文件內容

    第3次提交,新增hello.html文件 v2

    第4次提交,新增hello.html文件 v3

    第5次提交,新增hello.html文件 v4

    第6次提交,新增hello.html文件 v5

commit 5a5352de6d0f1704133dbb2e667153f72dca9ba7
Author: sun_wk <[email protected]>
Date:   Sat May 1 17:49:57 2021 +0800

    第2次提交,新增hello.html文件 v1

commit 69edafd3e11e4f3b8fea55eb0ff226824d4867fc
Author: sun_wk <[email protected]>
Date:   Sat May 1 17:46:43 2021 +0800

我們可以看到,最後四次提交合併爲一個提交了。

3、補充:合併多個不連續的提交

假如我們需要合併的多個提交,並不是連續的。

同樣的你也是要取得這幾個提交中,最早一次提交的父commit-id,作爲變基的基。

執行git rebase -i 父commit-id命令,然後進入交互界面中,然後把你需要合併的目標提交放在最頂部(格式:pick commit-id XXX,XXX就表示註釋,可有可無),然後把其他幾個要合併的提交編輯成s commit-id XXXX,最後保存退出即可。(之後其他的步驟和上面同理,這裏就不演示了。)

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