# git archive Command: Export Trees in CI

> git archive exports a tree as a tar or zip. Reference for --format, --prefix, and pathspecs to build clean source artifacts without .git in CI.

Source: https://latchkey.dev/learn/command-reference/git-archive-command-reference  
Updated: 2026-06-26

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.

## 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.

```.github/workflows/ci.yml
- 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 detached
```

> `git rev-parse --abbrev-ref HEAD` returns the literal string `HEAD` on a detached checkout rather than a branch name. On GitHub Actions read `github.ref_name` instead; the git command cannot know what it was checked out for.

## FAQ

### git archive Command: Export Trees in CI?

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.

### 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.

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
