Pre:
部署在内网的主机,无法联网,需要在离线的状态下进行更新.
方案:
使用git 的git format-patch或者 git bundle
两者的区别:
Which is better to use and why? git format-patch or git bundle?
git bundleonly makes sense if you transfer a changeset from one git-repository to another, whereasgit format-patchcreates standard diff-files which can be inspected easily and applied to a non-gitified source tree as well. on the other handgit bundlecreates a single file, which is easier to transport than the multi-file output ofgit format-patch
git bundle只产生一个文件,只能应用于同一个仓库,而git format-patch会产生多个文件,不仅限于同一个仓库.
git bundle:
使用场景:
- 
有可能你的网络中断了,但你又希望将你的提交传给你的合作者们。 
- 
可能你不在办公网中并且出于安全考虑没有给你接入内网的权限。 
- 
可能你的无线、有线网卡坏掉了。 
- 
可能你现在没有共享服务器的权限,你又希望通过邮件将更新发送给别人,却不希望通过 format-patch的方式传输 40 个提交。
bundle 命令会将 git push 命令所传输的所有内容打包成一个二进制文件,你可以将这个文件通过邮件或者闪存传给其他人,然后解包到其他的仓库中。
git bundle 使用命令:
创建:
生成repo.bundle的文件,该文件包含了所有重建该仓库master分支所需的数据
| 1 | git bundle create repo.bundle HEAD master | 
在使用 bundle 命令时,你需要列出所有你希望打包的引用或者提交的区间。 如果你希望这个仓库可以在别处被克隆,你应该像例子中那样增加一个 HEAD 引用。
应用:
克隆项目
| 1 | git clone repo.bundle repo | 
git format-patch:
UNIX世界的软件开发大多都是协作式的,因此,Patch(补丁)是一个相当重要的东西,因为几乎所有的大型UNIX项目的普通贡献者,都是通过 Patch来提交代码的。作为最重要的开源项目之一,Linux,也是这样的。普通开发者从软件仓库clone下代码,然后写入代码,做一个Patch, 最后用E-mail发给Linux Kernel的维护者就好了。Git最初作为Linux的版本控制工具,提供了透明、完整、稳定的Patch功能。
我们先介绍一下Patch是什么。如果一个软件有了新版本,我们可以完整地下载新版本的代码进行编译安装。然而,像Linux Kernel这样的大型项目,代码即使压缩,也超过70MB,每次全新下载是有相当大的代价的。然而,每次更新变动的代码可能不超过1MB,因此,我们只 要能够有两个版本代码的diff的数据,应该就可以以极低的代价更新程序了。因此,Larry Wall开发了一个工具:patch。它可以根据一个diff文件进行版本更新。
不过在git中,我们没有必要直接使用diff和patch来做补丁,这样做既危险又麻烦。git提供了两种简单的patch方案。一是用git diff生成的标准patch,二是git format-patch生成的Git专用Patch。
git format-patch 使用命令:
创建patch:
某次提交(含)之前的几次提交:n指从sha1 id对应的commit开始算起n个提交
| 1 | git format-patch 【commit sha1 id】-n | 
eg:
| 1 | git format-patch 2a2fb4539925bfa4a141fe492d9828d030f7c8a8 -2 | 
某两次提交之间的所有patch:
| 1 | git format-patch 【commit sha1 id】..【commit sha1 id】 | 
eg:
| 1 | git format-patch 2a2fb4539925bfa4a141fe492d9828d030f7c8a8..89aebfcc73bdac8054be1a242598610d8ed5f3c8 | 
应用patch:
检查patch/diff是否能正常打入:
| 1 | git apply --check 【path/to/xxx.patch】 | 
打入patch/diff:
| 1 | git apply 【path/to/xxx.patch】 | 
Summary:
git bundle可以理解为全量更新git format-patch可以理解为增量更新
为了方便,还是选择了git bundle,因为使用git format-patch的过程,容易出现patch打不上导致更新中断的问题.
不过对于更严谨的更新方案的话,还是应该选择git format-patch的.
大体思路:
- 
更新包生成端: - 分别生成各个项目的bundle文件
- 加密压缩成zip文件
 
- 分别生成各个项目的
- 
更新包使用端: - 用密钥解密压缩包
- 到指定目录移除原来的项目,执行git clone
 
