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
git clone Command: Clone Repos in CIgit clone copies a remote repository into a new directory. Reference for --depth, --branch, --filter, and --r…
git lfs Command: Large Files in CIgit lfs manages large files stored outside Git. Reference for git lfs install and git lfs pull to fetch large…
git tag Command: Create Tags in CIgit tag creates and lists tags. Reference for -a annotated tags and -l listing, used to cut releases and disc…
git ls-files Command: List Tracked Files in CIgit ls-files lists files in the index. Reference for listing tracked, ignored, or modified files to drive lin…