Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Remotes and Collaboration

git archive command

Reference for git archive: export the files of a commit or tree as tar, tar.gz or zip, add prefixes, limit paths, use export-ignore and export-subst, and embed the commit ID.

git archive writes the files of a tree (the snapshot behind a commit, tag or branch) as a tar or zip archive. The archive has no .git directory and no history, and only tracked files are included. That makes it the correct way to produce release tarballs and deployment packages.

Because it reads from the object database, the archive always matches the commit you name, regardless of uncommitted edits or untracked files in your working tree.

Terminal
git archive [--format=<fmt>] [--prefix=<prefix>/] [-o <file>] <tree-ish> [<path>...]

Try it

Bash List the contents of an archive

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash export-ignore and export-subst

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git archive [--format=<fmt>] [-l | --list] [-<level>] [--prefix=<prefix>/] [-o <file> | --output=<file>]
            [--add-file=<file>] [--add-virtual-file=<path>:<content>] [--worktree-attributes]
            [--remote=<repo> [--exec=<git-upload-archive>]] <tree-ish> [<path>...]
Option Description
<tree-ish> Commit, tag, branch or tree to export. Required.
<path>... Only export these files or directories.
--format=<fmt> tar, zip, or a configured format such as tar.gz / tgz. Default tar, unless -o has a recognized extension.
-o, --output=<file> Write to a file instead of standard output; the extension picks the format.
--prefix=<prefix>/ Prepend a path to every entry. End it with / to get a top directory.
-0 ... -9 Compression level for zip (and for configured compressors).
--add-file=<file> Add an untracked file from disk, for example a generated VERSION or build output. Repeatable.
--add-virtual-file=<path>:<content> Add a file with the given content (since 2.38).
--worktree-attributes Read attributes from the working tree instead of the commit. Useful for testing export-ignore.
--remote=<repo> Fetch the archive from a remote repository that allows it.
-l, --list Print the supported formats.

Returns: the archive on standard output or in the file. Errors: fatal: not a valid object name for an unknown revision; pathspec ... did not match any files for a missing path.

Usage notes

  • Tracked files only. Ignored and untracked files, and the .git folder, are never included. Use --add-file for generated files.
  • Attributes decide the rest. export-ignore removes files from the archive, export-subst expands $Format:...$ placeholders using git log --format codes. See export-ignore and export-subst.
  • Reproducibility. The archive uses the commit time for every entry's modification time, so the same commit gives the same bytes (gzip timestamps aside). That helps with checksums and signatures.
  • Commit ID in the file. When you archive a commit, git get-tar-commit-id can read it back from the tar header.
  • Submodules are not expanded: you get an empty folder where the gitlink is. Archive each submodule yourself and combine the results.
  • Compression. Configure tar.tar.gz.command for custom compressors (tar.<format>.command). Piping through gzip -n or xz is the usual manual way.
  • .gitattributes is read from the tree you export, not from the work tree, unless --worktree-attributes is set.