背景
正常情况下,开发完代码提交后,会通过git push推送到远端仓库。
但是如果由于各种网络原因无法连接到仓库,git bundle可以充当离线版的git push,它可以将git push命令所传输的所有内容打包成bundle文件。(意味着.gitignore文件是会起作用的。)
这个bundle文件可以理解为“离线的可移动的中转仓库”,它包含了所有重建源头仓库所需的数据,可以把它共享给项目协作者,对方可以通过clone/pull/fetch来使用。
命令简介
git bundle create <file> <git-rev-list-args>
git bundle verify <file>
git bundle list-heads <file> [<refname>…]
create - 创建
这个操作可以将整个分支或指定区间内的提交打包成文件。
示例如下:
git bundle create test.bundle HEAD master
git bundle create test.bundle HEAD dev_v1.0.0 ^master
git bundle create test.bundle HEAD HEAD~2..HEAD
[wangl29@localhost k8s-install]$ git bundle create ../k8s-install.bundle HEAD master obsbuild
Enumerating objects: 198, done.
Counting objects: 100% (198/198), done.
Compressing objects: 100% (185/185), done.
Total 198 (delta 104), reused 0 (delta 0), pack-reused 0
verify - 校验
用来校验生成的包是否合法。此命令需要在项目代码仓库目录下执行,否则会报错。
操作实例:
[wangl29@localhost k8s-install]$ git bundle verify ../k8s-install.bundle
The bundle contains these 3 refs:
c2411160ddf5e625238eb7da48276df178e90ec2 HEAD
c2411160ddf5e625238eb7da48276df178e90ec2 refs/heads/master
135274110e45901f32536b40afa64ede1e5447ae refs/heads/obsbuild
The bundle records a complete history.
../k8s-install.bundle is okay
list-heads - 列出顶端引用
列出 bundle 文件中定义的顶端引用。可以看到哪些分支被打到文件里。
操作实例:
[wangl29@localhost k8s-install]$ git bundle list-heads ../k8s-install.bundle
c2411160ddf5e625238eb7da48276df178e90ec2 HEAD
c2411160ddf5e625238eb7da48276df178e90ec2 refs/heads/master
135274110e45901f32536b40afa64ede1e5447ae refs/heads/obsbuild
离线仓库取用
当拿到了bundle文件,便可以通过clone/pull/fetch按需使用了。
张玉川@LAPTOP-708QICE9 MINGW64 /c/workspace/git
$ git clone k8s-install.bundle
Cloning into 'k8s-install'...
Receiving objects: 100% (198/198), 79.76 KiB | 19.94 MiB/s, done.
Resolving deltas: 100% (104/104), done.
cd到仓库,可以看到源头仓库的分支信息,提交历史都完整呈现了。
张玉川@LAPTOP-708QICE9 MINGW64 /c/workspace/git
$ cd k8s-install
张玉川@LAPTOP-708QICE9 MINGW64 /c/workspace/git/k8s-install (master)
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/obsbuild
总结
简单来说,git bundle 的使用场景其实可以理解为:
在网络受限无法直连仓库的情况下,将本地数据推送(打包)到一个 bundle 文件,然后通过邮件或其它方式传送给需要的人,对方在更新的时候,拉取更新的来源是你的 bundle 文件而不是远端仓库,因为此时 bundle 文件就相当于一个离线的中转仓库。