Synopsis
git archive [--format=<fmt>] [--prefix=<p>/] [-o <file>] <tree-ish> [<path>...]
Description
The git archive command creates a tar or zip file containing the contents of any tree (commit, tag, or branch). Unlike a simple tar of the working tree, it includes only tracked files at a specific revision and excludes .git directories. This makes it ideal for producing release tarballs, source distributions, and reproducible snapshots.
The --prefix option wraps all files under a subdirectory inside the archive — important for releases so that extraction creates a named folder. git archive also supports running over the network from servers that allow it.
In day-to-day use, git archive integrates closely with shell aliases, editor plugins, and continuous integration. Power users often add aliases that combine flags they always pass, or wrap the command in scripts that enforce team conventions. Output formatting can be customized via Git config — pretty formats, color schemes, and pager behavior are all tunable. When something goes wrong, the first diagnostic step is usually to re-run the command with GIT_TRACE=1 in the environment, which reveals the underlying plumbing calls. For unusual situations, the --help output (git archive --help) opens the full manual page with details on every option, including those rarely used in casual workflows but essential for debugging or scripting at scale.
Understanding how git archive interacts with the rest of Git's data model — the object database, the index, refs, and the working tree — pays dividends. Each command operates on some subset of these pieces, and knowing which it touches helps predict outcomes and recover from mistakes. Reading the official Git documentation alongside hands-on practice in a throwaway repository is the fastest way to internalize the nuances. Most production issues with Git stem from one of three causes: surprising default behavior, partial network operations, or rewriting history that was already shared. A working mental model of git archive's side effects helps avoid all three.
Common Options
| Option | Description |
|---|---|
--format=<fmt> | tar, tar.gz, zip (default tar). |
--prefix=<p>/ | Prepend a directory to all archive paths. |
-o <file> | Write to file instead of stdout. |
--remote=<repo> | Archive from a remote repository. |
--worktree-attributes | Honor export-ignore attributes. |
-l, --list | List supported formats. |
Examples
git archive --format=tar.gz --prefix=myproj-1.0/ -o myproj-1.0.tar.gz v1.0
# Release tarball with named top-level directory
git archive HEAD docs/ | tar -x -C /tmp/docs
# Extract the docs/ directory at HEAD into /tmp/docs
git archive --format=zip -o snapshot.zip HEAD
# ZIP archive of current tree
Examples (continued)
# Use .gitattributes to exclude files from archives:
echo "tests/ export-ignore" >> .gitattributes
echo ".github/ export-ignore" >> .gitattributes
git archive --worktree-attributes -o release.tar.gz HEAD
Common Mistakes
Forgetting --prefix creates an archive that explodes files into the current directory on extraction. Without export-ignore attributes, your archive may include CI configs, tests, and dev-only files that you didn't intend to ship.
Related Commands
git bundle, git tag, git show, tar