LinuxCommandLibrary

git-archive

Create an archive of a Git repository

TLDR

Create a .tar archive from the contents of the current HEAD and print it to stdout

$ git archive [[-v|--verbose]] HEAD
copy

Use the Zip format and report progress verbosely
$ git archive [[-v|--verbose]] --format zip HEAD
copy

Output the Zip archive to a specific file
$ git archive [[-v|--verbose]] [[-o|--output]] [path/to/file.zip] HEAD
copy

Create a .tar archive from the contents of the latest commit of a specific branch
$ git archive [[-o|--output]] [path/to/file.tar] [branch_name]
copy

Use the contents of a specific directory
$ git archive [[-o|--output]] [path/to/file.tar] HEAD:[path/to/directory]
copy

Prepend a path to each file to archive it inside a specific directory
$ git archive [[-o|--output]] [path/to/file.tar] --prefix [path/to/prepend]/ HEAD
copy

SYNOPSIS

git archive [--format=<fmt>] [--prefix=<prefix>/] [-o <file>] [--remote=<repo>] [--worktree-attributes] [<tree-ish>] [--] [<path>...] [<extra>...]

PARAMETERS

--format=<fmt>
    Set archive format (e.g., tar, zip). Default: tar.

--list
    List all supported archive formats.

--prefix=<prefix>/
    Prepend prefix/ to each pathname in the archive.

--remote=<repo>
    Fetch archive from remote repository instead of local.

-o <file>, --output=<file>
    Write archive to file instead of stdout.

-w <dir>
    Equivalent to --worktree-attributes with working tree dir.

--worktree-attributes
    Read .gitattributes from working tree, not index.

--export-tree=<oid>
    Export specific tree object. Incompatible with --remote.

DESCRIPTION

git archive is a versatile Git command for generating archives (tar or zip) directly from repository trees without extracting files to disk. It packages specified files or the entire tree-ish (e.g., HEAD, tag, commit SHA) into a single archive file, ideal for source distributions, releases, or backups.

Key features include path filtering to include only certain directories/files, custom prefixes for pathnames, and output redirection to files. It supports local repositories natively and remote ones via the --remote option, fetching archives over the network if the server supports it.

Archives exclude the .git directory by default, producing clean distributions. Format-specific options (e.g., compression) can be passed via <extra> arguments. For reproducible builds, combine with --worktree-attributes to embed attributes from the working tree.

Common use cases: creating project tarballs for uploads, exporting tagged releases as zip files, or piping to compressors like gzip. It's efficient for large repos as it streams content without full checkouts.

CAVEATS

Remote archives require server-side git-archive support; not all hosts enable it.
<extra> args are format-specific and order-sensitive.
Zip archives may not preserve all Git attributes or permissions perfectly.

COMMON EXAMPLES

Local tarball: git archive --format=tar --prefix=proj/ -o proj.tar HEAD
Zip release: git archive --format=zip --output=release.zip v1.0
Pipe to gzip: git archive HEAD | gzip > archive.tar.gz
Remote: git archive --remote=origin --format=tar HEAD

FORMATS DETAILS

tar: Supports <extra> like -J (xz), -z (gzip).
zip: Deflate compression; <extra> like -0 (store only). Custom formats via plugins.

HISTORY

Introduced in Git 1.4.0 (April 2006) for basic tar support. Zip added in 1.7.1 (2010). Remote fetching and --worktree-attributes in later 1.x releases. Stabilized in Git 2.x with export-tree and enhanced formats.

SEE ALSO

tar(1), zip(1), gzip(1), git-bundle(1), git-diff-tree(1)

Copied to clipboard