柚子快報(bào)邀請(qǐng)碼778899分享:【Git 學(xué)習(xí)筆記
柚子快報(bào)邀請(qǐng)碼778899分享:【Git 學(xué)習(xí)筆記
文章目錄
第四章 變基操作及相關(guān)案例4.0 變基簡(jiǎn)介4.1 將 `commit` 版本變基到另一分支4.2 在版本沖突的情況下執(zhí)行變基4.3 對(duì)指定版本執(zhí)行交互式變基
第四章 變基操作及相關(guān)案例
【相關(guān)主題】
將 commit 版本變基到另一分支在版本沖突的情況下執(zhí)行變基對(duì)指定版本執(zhí)行交互式變基利用交互式變基聚合版本利用交互式變基變更提交者自動(dòng)聚合版本
4.0 變基簡(jiǎn)介
變基(Rebasing)是 Git 具備的一個(gè)異常強(qiáng)大的特性。變基是這樣一類(lèi)操作:假如一個(gè) commit A 最早是基于 commit B 的;那么將 A 變基到 C,就是將 A 變?yōu)榛?C 的操作。
在接下來(lái)的演示案例中,你會(huì)發(fā)現(xiàn)變基操作往往并不像看上去那么容易。
4.1 將 commit 版本變基到另一分支
先來(lái)看看最簡(jiǎn)單的一類(lèi)變基操作。相關(guān)準(zhǔn)備工作:引入一個(gè)新文件、提交、變更內(nèi)容、再提交。這樣本地就有了兩次 commit 版本。
還是以 jgit 庫(kù)為例:
# Clone jgit repo into chapter4
$ git clone https://git.eclipse.org/r/jgit/jgit chapter4
$ cd chapter4
# Checkout a new branch
$ git checkout -b rebaseExample --track origin/stable-3.1
# Create the 1st commit
$ echo "My Fishtank
Gravel, water, plants
Fish, pump, skeleton" > fishtank.txt
$ git add fishtank.txt
$ git commit -m "My brand new fishtank"
# Create the 2nd commit
$ echo "mosquitos" >> fishtank.txt
$ git add fishtank.txt
$ git commit -m "Feeding my fish"
# Rabase to stable-3.2
$ git rebase origin/stable-3.2
Successfully rebased and updated refs/heads/rebaseExample.
變基前:
變基后:
git rebase 的執(zhí)行過(guò)程:
找到 HEAD 與變基指向的目標(biāo)分支之間的公共版本(merge-base);基于 merge-base,找出目標(biāo)分支上所有缺少的版本;嘗試將缺少的版本逐一應(yīng)用到目標(biāo)分支上。
4.2 在版本沖突的情況下執(zhí)行變基
如果將一個(gè) commit 版本或一個(gè) branch 分支變基到不同的 HEAD 上,很可能會(huì)出現(xiàn)版本沖突。此時(shí)必須解決完沖突,并運(yùn)行命令 git rebase --continue,方可完成變基。
本節(jié)示例將演示如何在有沖突的情況下完成變基。沿用 4.1 節(jié)中演示的最終結(jié)果,此時(shí) rebaseExample 分支已經(jīng)變基到 stable-3.2 分支。示例將從 stable-3.1 重新檢出新分支,并添加一個(gè)和 rebaseExample 分支同名但內(nèi)容不容的文本文件 fishtank.txt:
# Checkout rebaseExample2
$ git checkout -b rebaseExample2 --track origin/stable-3.1
# Add a new commit
$ echo "My Fishtank
Pirateship, Oister shell
Coconut shell
">fishtank.txt
$ git add fishtank.txt
$ git commit -m "My brand new fishtank"
# Rebase conflicting branches
$ git rebase rebaseExample
Auto-merging fishtank.txt
CONFLICT (add/add): Merge conflict in fishtank.txt
error: could not apply 24f9bf1ef... My brand new fishtank2
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 24f9bf1ef... My brand new fishtank2
$ subl fishtank.txt
沖突情況如下:
改為如下內(nèi)容后保存、關(guān)閉:
添加合并好的文件,繼續(xù)變基:
$ git add fishtank.txt
$ git rebase --continue
hint: Waiting for your editor to close the file...
打開(kāi)的編輯器如圖所示:
確認(rèn)、保存并關(guān)閉,將看到提示變基成功:
$ git rebase --continue
[detached HEAD 9911772b3] My brand new fishtank2
1 file changed, 2 insertions(+), 3 deletions(-)
Successfully rebased and updated refs/heads/rebaseExample2.
# Check new status via gitk
$ gitk
詳情如下:
可見(jiàn),本次變基,只將舊分支有、而新分支沒(méi)有的版本變基過(guò)來(lái)(即新增 commit)。
在首次變基中斷的 git 提示信息中,還能看到兩個(gè)備選項(xiàng):
git rebase --abort:如字面含義,中斷變基 git rebase --skip:跳過(guò)沖突直接變基,這將導(dǎo)致 rebaseExample2 放棄新增的 commit,直接并入 rebaseExample,變基后指向的父級(jí),和 rebaseExample 一致:
4.3 對(duì)指定版本執(zhí)行交互式變基
本例以 4.1 中的 rebaseExample 分支為基礎(chǔ),演示如何利用 --interactive 標(biāo)記,將 4.1 中變基的兩個(gè) commit 重新變基到遠(yuǎn)程跟蹤分支 stable-3.1 上:
$ git checkout rebaseExample
Switched to branch 'rebaseExample'
Your branch is ahead of 'origin/stable-3.1' by 109 commits.
(use "git push" to publish your local commits)
$ git rebase origin/stable-3.1
此時(shí)在新彈出的編輯器中會(huì)展示一個(gè) commit 列表,范圍是 rebaseExample 與 stable-3.1 之間的、所有可以變基到 stable-3.1 的 commit 記錄。保留示例中新增的兩個(gè) commit,其余全部刪除(即第 89 行及以前的內(nèi)容):
保存后退出編輯器可以看到如下輸出:
$ git rebase --interactive origin/stable-3.1
Successfully rebased and updated refs/heads/rebaseExample.
# Check in gitk view:
結(jié)果如下:
示例拓展
本例還可以通過(guò)一個(gè)命令快速實(shí)現(xiàn)既定效果:
$ git rebase --onto origin/stable-3.1 origin/stable-3.2 rebaseExample
Successfully rebased and updated refs/heads/rebaseExample.
變基前后示意圖如下:
柚子快報(bào)邀請(qǐng)碼778899分享:【Git 學(xué)習(xí)筆記
推薦鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。