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

(二)連續(xù)多個提交合并成一個提交

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

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

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

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

我們可以看到下面歷史提交情況,發(fā)現(xiàn)最新生成的幾個commit都是修改hello.html文件的內(nèi)容,那我們可以把這幾個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、執(zhí)行commit合并(重點)

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

說明:

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

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

進入到交互頁面中,看看里邊的內(nèi)容。

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的提交內(nèi)容,合并到策略為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第二個交互頁面

上邊保存退出之后,會直接跳轉(zhuǎn)到這個交互頁面上。

如下:

# 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文件內(nèi)容

第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中是不會出現(xiàn)的。

保存退出(:wq!)。

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

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

說明:rebase操作已經(jīng)成功,并且分支頭指針,也指向了最新的commit。

(原理同上面應用,這里就不重復了)

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

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git log --oneline
840f01d (HEAD -> master) 修改hello.html文件內(nèi)容
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 <sun_wk@126.com>
Date:   Sat May 1 17:50:22 2021 +0800

    修改hello.html文件內(nèi)容

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

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

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

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

commit 5a5352de6d0f1704133dbb2e667153f72dca9ba7
Author: sun_wk <sun_wk@126.com>
Date:   Sat May 1 17:49:57 2021 +0800

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

commit 69edafd3e11e4f3b8fea55eb0ff226824d4867fc
Author: sun_wk <sun_wk@126.com>
Date:   Sat May 1 17:46:43 2021 +0800

我們可以看到,最后四次提交合并為一個提交了。

3、補充:合并多個不連續(xù)的提交

假如我們需要合并的多個提交,并不是連續(xù)的。

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

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容