git 制作增量包(差异包)

打增量包无非 “选文件”, “打包” 两个操作

1.筛选差异文件

git 比较差异当然是 git diff 命令了

语法如下:

1
2
3
4
5
git diff [options] [<commit>] [--] [<path>…​]
git diff [options] --cached [<commit>] [--] [<path>…​]
git diff [options] <commit> <commit> [--] [<path>…​]
git diff [options] <blob> <blob>
git diff [options] [--no-index] [--] <path> <path>

options 有这么一个参数

1
2
--name-only  
Show only names of changed files.

可以试一下, 比较本地 masterorigin/master 的差异

1
2
3
> git diff master origin/master --name-only
wp/wp-content/themes/alcatel/csr.php
wp/wp-content/themes/alcatel/service_privacy_policy.php

OK 这就是我们需要的

2.打包

有了文件列表,打包就好办了

tar 命令打包

1
> tar -zcvf diff.tar.gz $(git diff master origin/master --name-only)

zip 命令打包

1
> zip -q -r diff.zip $(git diff master origin/master --name-only)

git archive 命令打包

其实最实用的还是直接用git的归档命令打包
好处就是不需要额外东西 windows下的 PowerShellgit bash 也能用

语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
git archive [<options>] <tree-ish> [<path>...]
git archive --list
git archive --remote <repo> [--exec <cmd>] [<options>] <tree-ish> [<path>...]
git archive --remote <repo> [--exec <cmd>] --list

--format <fmt> archive format
--prefix <prefix> prepend prefix to each pathname in the archive
-o, --output <file> write the archive to this file
--worktree-attributes
read .gitattributes in working directory
-v, --verbose report archived files on stderr
-0 store only
-1 compress faster
-9 compress better

-l, --list list supported archive formats

--remote <repo> retrieve the archive from remote repository <repo>
--exec <command> path to the remote git-upload-archive command

archive 命令支持的打包格式可以用 git archive -l 查看
正常情况可以省略 --format ,git会根据输出的文件名判断格式

打包代码如下:

1
> git archive -o diff.zip master $(git diff master origin/master --name-only)

linux 还可以用上管道符号

1
> git archive master $(git diff master origin/master --name-only) | gzip > diff.zip

注意:如果 diff 命令结果是空 archive 会打包整个项目

3.常用增量包命令

当前分支与master差异包

1
> git archive -o update.zip HEAD $(git diff master --name-only)

最后一次commit差异包

1
> git archive -o lastcommit.zip HEAD $(git diff HEAD^ --name-only)

x 中提取 xy 的差异文件打包.

x , y 可以是 分支, tag, commit

1
> git archive -o x_y.zip x $(git diff x y --name-only)