使用git对代码进行打包的方法

1. 说明

通常我在发布代码的时候会先打tag,然后下载该tag进行发布。实际上tag更多的是用于标记版本以方便管理,git提供了多个打包代码的命令帮助用户发布代码。

2. 打包分支代码

使用 git archive 可以将指定分支的代码进行打包,支持.zip, .tar 和 .tar.gz 格式的代码包,使用这个命令打包的代码只包含当前版本的代码,像log信息之类的并不包含,也不能在目录下使用git 相关的命令。

比如打包 master 分支:

git archive master --format=tar.gz --output=/home/raymond/work/master.tar.gz

默认情况下是.tar 格式压缩包,通过 --format 参数来选择其他格式, --output指定输出的文件名称。

2.1 git archive 命令

其命令格式如下:

git archive [--format=<fmt>] [--list] [--prefix=<prefix>/] [<extra>]
	      [-o <file> | --output=<file>] [--worktree-attributes]
	      [--remote=<repo> [--exec=<git-upload-archive>]] <tree-ish>
	      [<path>…​]

各项参数:

  • --format=<fmt>

Format of the resulting archive: tar or zip. If this option is not given, and the output file is specified, the format is inferred from the filename if possible (e.g. writing to "foo.zip" makes the output to be in the zip format). Otherwise the output format is tar.

  • -l, --list

Show all available formats.

  • -v, --verbose

Report progress to stderr.

  • --prefix=<prefix>/

Prepend <prefix>/ to each filename in the archive.

  • -o <file>, --output=<file>

Write the archive to <file> instead of stdout.

  • --worktree-attributes

Look for attributes in .gitattributes files in the working tree as well (see the section called “ATTRIBUTES”).

  • <extra>

This can be any options that the archiver backend understands. See next section.

  • --remote=<repo>

Instead of making a tar archive from the local repository, retrieve a tar archive from a remote repository. Note that the remote repository may place restrictions on which sha1 expressions may be allowed in <tree-ish>. See git-upload-archive(1) for details.

  • --exec=<git-upload-archive>

Used with --remote to specify the path to the git-upload-archive on the remote side.

  • <tree-ish>

The tree or commit to produce an archive for.

  • <path>

Without an optional path parameter, all files and subdirectories of the current working directory are included in the archive. If one or more paths are specified, only these are included.

3. 带 log 信息打包分支

如果打包时需要将log信息等内容一并打包进目录,那么可以使用 git bundle 命令进行打包。该命令会将指定分支的.git信息一并打包,在解压后的目录下可以正常使用 git 命令进行查看log等操作,但是不能切换分支。

打包分支

git bundle create master.bundle /home/raymond/work/master

解包分支 

git clone master.bundle ./sample_repo -b master

解包时要求存放代码的目录为空目录。

3.1  git bundle 命令

git bundle create [-q | --quiet | --progress | --all-progress] [--all-progress-implied] \
                    <file> <git-rev-list-args>
git bundle verify [-q | --quiet] <file>
git bundle list-heads <file> [<refname>...]
git bundle unbundle <file> [<refname>...]

一些工作要求将一台机器上的一个或多个开发分支复制到另一台机器上,但两台机器不能直接连接,因此无法使用交互式 Git 协议(git、ssh、http)。该命令提供对 git fetch 和 git pull 的支持,通过在源机器上将对象和引用打包到存档中,然后在通过某种方式移动存档后使用 git fetch 和 git pull 将它们导入另一个存储库.由于存储库之间不存在直接连接,因此用户必须为目标存储库持有的捆绑包指定基础:捆绑包假定基础中的所有对象都已在目标存储库中。

 各项参数:

  • create <file>

Used to create a bundle named file. This requires the git-rev-list-args arguments to define the bundle contents.

  • verify <file>

Used to check that a bundle file is valid and will apply cleanly to the current repository. This includes checks on the bundle format itself as well as checking that the prerequisite commits exist and are fully linked in the current repository. git bundle prints a list of missing commits, if any, and exits with a non-zero status.

  • list-heads <file>

Lists the references defined in the bundle. If followed by a list of references, only references matching those given are printed out.

  • unbundle <file>

Passes the objects in the bundle to git index-pack for storage in the repository, then prints the names of all defined references. If a list of references is given, only references matching those in the list are printed. This command is really plumbing, intended to be called only by git fetch.

  • <git-rev-list-args>

A list of arguments, acceptable to git rev-parse and git rev-list (and containing a named ref, see SPECIFYING REFERENCES below), that specifies the specific objects and references to transport. For example, master~10..master causes the current master reference to be packaged along with all objects added since its 10th ancestor commit. There is no explicit limit to the number of references and objects that may be packaged.

  • [<refname>…​]

A list of references used to limit the references reported as available. This is principally of use to git fetch, which expects to receive only those references asked for and not necessarily everything in the pack (in this case, git bundle acts like git fetch-pack).

参考链接

Git - git-archive Documentation

Git - git-bundle Documentation

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翔底

您的鼓励将是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值