Export a Git snapshot without the .git directory

Use git archive to export a commit or subdirectory as ZIP or tar, apply export-ignore rules, and avoid packaging the wrong working-tree contents.

Best practices
Illustration of Git branches and version history

Use git archive to export the files stored at a commit, branch, or tag without including .git. It is Git's usual equivalent to exporting a versioned source snapshot with svn export.

Create an archive

git archive --format=zip -o release.zip HEAD
git archive --format=tar.gz -o v1.2.tar.gz v1.2.0

The second example requires a v1.2.0 tag. Supported formats can be listed with git archive --list.

For a release archive, add a common top-level directory:

git archive --format=tar.gz --prefix=myapp-1.2.0/ \
  -o myapp-1.2.0.tar.gz v1.2.0

This exports the recorded tree, not uncommitted edits or generated build output. If the deliverable needs compilation, treat source export and artifact creation as separate steps.

Export into a directory

Create an empty destination before extracting:

mkdir -p /tmp/api-only
git archive HEAD:src/api | tar -x -C /tmp/api-only

HEAD:src/api exports the directory's contents at the archive root. To export the entire commit, use git archive HEAD | tar -x -C /path/to/target with an existing destination. Check that extraction will not overwrite files you want to keep.

Export from a remote

A server that supports Git's upload-archive service can accept:

git archive --remote=ssh://git@example.com/team/repo.git \
  --format=tar -o snapshot.tar HEAD

Support depends on the server. GitHub does not provide this Git protocol service; use its Code → Download ZIP action or a repository archive URL instead:

curl -fL -o main.zip https://github.com/org/repo/archive/refs/heads/main.zip

FAQ

Does .gitignore control the archive?

The selected tree controls inclusion. A tracked file is still eligible even if an ignore rule matches it. Use the export-ignore attribute in .gitattributes to omit tracked paths from exports, such as tests or development-only files.

Can I export only files changed between two revisions?

A list from git diff --name-only A B identifies changed paths, but feeding that list to zip packages the current working-tree files, not necessarily revision B; deleted paths also require handling. For a reproducible transfer of changes, use git diff --binary A B > changes.patch, or build a dedicated export that reads changed, nondeleted paths from B and records deletions separately. A normal archive of B is simpler when a full snapshot is acceptable.

Does the archive include submodule contents?

A normal superproject archive does not recursively export the repositories referenced by submodules. Account for them explicitly. Also check Git LFS requirements: a local Git tree may contain pointer files rather than the large objects needed by the consumer.

Why not copy the working directory and delete .git?

That can include uncommitted changes, ignored files, and local secrets. An archive names the recorded version explicitly, making its source easier to verify.

See git-archive, git-diff, and GitHub repository archives.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting