Skip to content
Latchkey

git archive Command: Export Trees in CI

git archive packages the contents of a tree into a tar or zip file, excluding Git metadata.

Release pipelines use git archive to produce a clean source tarball: exactly the tracked files at a ref, with no .git directory or untracked junk. It honors export-ignore attributes too.

Common flags

  • --format=tar|zip|tar.gz - choose the output format
  • --prefix=<dir>/ - prepend a directory to every path in the archive
  • -o <file> / --output - write to a file instead of stdout
  • <tree-ish> - the commit, branch, or tag to archive
  • -- <pathspec> - limit the archive to matching paths
  • --worktree-attributes - apply working-tree .gitattributes (export-ignore)

Example

shell
# Build a clean, prefixed release tarball from a tag
git archive --format=tar.gz --prefix="app-${VERSION}/" \
  -o "app-${VERSION}.tar.gz" "v${VERSION}"

In CI

git archive is the right way to ship source artifacts: it includes only tracked files at the chosen ref, never the .git directory or local cruft, so output is reproducible. Use export-ignore .gitattributes to drop test fixtures or CI files from the release tarball.

Key takeaways

  • git archive exports a clean, reproducible tarball of tracked files at a ref.
  • --prefix nests everything under a versioned directory for tidy extraction.
  • export-ignore .gitattributes let you exclude files from the release artifact.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →