首页·终端命令

git archive

git

用法

git archive --format=tar.gz -o <file> <tree-ish>

把某个提交时的文件导出成 tar 或 zip;不会带上 .git 和未跟踪文件,所以比直接压缩文件夹更干净。

常用选项

选项含义
--format=<fmt>tar, zip or tar.gz
-o <file>write to a file; the format is guessed from the name
--prefix=<dir>/put everything under one folder inside the archive
--add-file=<file>include an untracked file as well
--remote=<repo>let the server build it
-llist the formats this build supports

示例

git archive --format=tar.gz -o app-1.2.0.tar.gz v1.2.0

a release tarball of exactly that tag

git archive --prefix=app/ -o app.zip HEAD

a zip whose contents sit inside app/

git archive -o src.zip HEAD -- src

only the src directory

难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. git archive 是做什么的?

把某个提交时的文件导出成 tar 或 zip;不会带上 .git 和未跟踪文件,所以比直接压缩文件夹更干净。

Q. 怎么写?

git archive --format=tar.gz -o <file> <tree-ish> —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 6 个,完整列表在 man git。这条命令属于git。

相关命令

man 手册: man git-archive