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
# 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.
Using this in CI
CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.
- uses: actions/checkout@v4
with:
fetch-depth: 0 # history, tags, and git describe all need this
- run: |
git rev-parse --is-shallow-repository # expect false
git rev-parse --abbrev-ref HEAD # prints HEAD when detachedKey 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.