Git筆記(36) 打包

Git筆記(36) 打包


1. 打包

雖然我們已經瞭解了網絡傳輸 Git 數據的常用方法(如 HTTP,SSH 等)
但還有另外一種不太常見卻又十分有用的方式
Git 可以將它的數據“打包”到一個文件中
這在許多場景中都很有用

有可能你的網絡中斷了,但你又希望將你的提交傳給你的合作者們
可能你不在辦公網中並且出於安全考慮沒有給你接入內網的權限
可能你的無線、有線網卡壞掉了
可能你現在沒有共享服務器的權限,又希望通過郵件將更新發送給別人
卻不希望通過 format-patch 的方式傳輸 40 個提交

這些情況下 git bundle 就會很有用

bundle 命令會將 git push 命令所傳輸的所有內容打包成一個二進制文件
可以將這個文件通過郵件或者閃存傳給其他人,然後解包到其他的倉庫中


2. 舉例

來看看一個簡單的例子
假設你有一個包含兩個提交的倉庫:

$ git log
commit 9a466c572fe88b195efd356c3f2bbeccdb504102
Author: Scott Chacon <[email protected]>
Date:   Wed Mar 10 07:34:10 2010 -0800

    second commit

commit b1ec3248f39900d2a406049d762aa68e9641be25
Author: Scott Chacon <[email protected]>
Date:   Wed Mar 10 07:34:01 2010 -0800

    first commit

如果你想把這個倉庫發送給其他人但你沒有其他倉庫的權限,或者就是懶得新建一個倉庫
你就可以用 git bundle create 命令來打包

$ git bundle create repo.bundle HEAD master
Counting objects: 6, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (6/6), 441 bytes, done.
Total 6 (delta 0), reused 0 (delta 0)

然後就會有一個名爲 repo.bundle 的文件,該文件包含了所有重建該倉庫 master 分支所需的數據
在使用 bundle 命令時,需要列出所有你希望打包的引用或者提交的區間

如果你希望這個倉庫可以在別處被克隆,你應該像例子中那樣增加一個 HEAD 引用

你可以將這個 repo.bundle 文件通過郵件或者U盤傳給別人

另一方面,假設別人傳給你一個 repo.bundle 文件並希望你在這個項目上工作
你可以從這個二進制文件中克隆出一個目錄,就像從一個 URL 克隆一樣

$ git clone repo.bundle repo
Cloning into 'repo'...
...
$ cd repo
$ git log --oneline
9a466c5 second commit
b1ec324 first commit

如果你在打包時沒有包含 HEAD 引用,還需要在命令後指定一個 -b master 或者其他被引入的分支
否則 Git 不知道應該檢出哪一個分支

現在假設你提交了 3 個修訂,並且要用郵件或者U盤將新的提交放在一個包裏傳回去

$ git log --oneline
71b84da last commit - second repo
c99cf5b fourth commit - second repo
7011d3d third commit - second repo
9a466c5 second commit
b1ec324 first commit

首先需要確認我們希望被打包的提交區間
和網絡協議不太一樣,網絡協議會自動計算出所需傳輸的最小數據集,而需要手動計算
當然你可以像上面那樣將整個倉庫打包
但最好僅僅打包變更的部分 —— 就是我們剛剛在本地做的 3 個提交

爲了實現這個目標,需要計算出差別
就像我們在 提交區間 介紹的,你有很多種方式去指明一個提交區間
可以使用 origin/master..master 或者 master ^origin/master 之類的方法
來獲取那 3 個在我們的 master 分支而不在原始倉庫中的提交

可以用 log 命令來測試。

$ git log --oneline master ^origin/master
71b84da last commit - second repo
c99cf5b fourth commit - second repo
7011d3d third commit - second repo

這樣就獲取到我們希望被打包的提交列表,讓我們將這些提交打包
可以用 git bundle create 命令,加上我們想用的文件名,以及要打包的提交區間

$ git bundle create commits.bundle master ^9a466c5
Counting objects: 11, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (9/9), 775 bytes, done.
Total 9 (delta 0), reused 0 (delta 0)

現在在目錄下會有一個 commits.bundle 文件
如果我們把這個文件發送給我們的合作者,她可以將這個文件導入到原始的倉庫中
即使在這期間已經有其他的工作提交到這個倉庫中

當她拿到這個包時,她可以在導入到倉庫之前查看這個包裏包含了什麼內容
bundle verify 命令可以檢查這個文件是否是一個合法的 Git 包,是否擁有共同的祖先來導入

$ git bundle verify ../commits.bundle
The bundle contains 1 ref
71b84daaf49abed142a373b6e5c59a22dc6560dc refs/heads/master
The bundle requires these 1 ref
9a466c572fe88b195efd356c3f2bbeccdb504102 second commit
../commits.bundle is okay

如果打包工具僅僅把最後兩個提交打包,而不是三個,原始的倉庫是無法導入這個包的
因爲這個包缺失了必要的提交記錄
這時候 verify 的輸出類似:

$ git bundle verify ../commits-bad.bundle
error: Repository lacks these prerequisite commits:
error: 7011d3d8fc200abe0ad561c011c3852a4b7bbe95 third commit - second repo

而我們的第一個包是合法的,所以我們可以從這個包裏提取出提交
如果你想查看這邊包裏可以導入哪些分支,同樣有一個命令可以列出這些頂端:

$ git bundle list-heads ../commits.bundle
71b84daaf49abed142a373b6e5c59a22dc6560dc refs/heads/master

verify 子命令同樣可以告訴你有哪些頂端

該功能的目的是查看哪些是可以被拉入的
所以你可以使用 fetch 或者 pull 命令從包中導入提交
這裏我們要從包中取出 master 分支到我們倉庫中的 other-master 分支:

$ git fetch ../commits.bundle master:other-master
From ../commits.bundle
 * [new branch]      master     -> other-master

可以看到我們已經將提交導入到 other-master 分支,以及在這期間我們自己在 master 分支上的提交

$ git log --oneline --decorate --graph --all
* 8255d41 (HEAD, master) third commit - first repo
| * 71b84da (other-master) last commit - second repo
| * c99cf5b fourth commit - second repo
| * 7011d3d third commit - second repo
|/
* 9a466c5 second commit
* b1ec324 first commit

因此,當你在沒有合適的網絡或者可共享倉庫的情況下
git bundle 很適合用於共享或者網絡類型的操作


參考: git
以上內容,均根據git官網介紹刪減、添加和修改組成


相關推薦:

Git筆記(35) 子模塊
Git筆記(34) 調試
Git筆記(33) Rerere
Git筆記(32) 高級合併
Git筆記(31) 重置揭密


謝謝

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