How to create an archive from a Git repository

Packaging source from Git is different from copying a working tree, because a release archive should contain the tracked files from one chosen revision and leave repository metadata behind. git archive creates that handoff artifact from a commit, tag, branch, or tree without including the .git directory.

The command reads a named tree and writes an archive in a supported format such as tar, zip, tar.gz, or tgz. A top-level prefix such as project/ keeps extraction organized, and a specific revision such as HEAD or a release tag makes the packaged file repeatable.

Only committed tracked files are included unless extra archive options add files explicitly. Committed .gitattributes rules such as export-ignore can remove files from the archive, so list the archive before sharing it and avoid unsafe prefixes such as absolute paths or ../.

Steps to create a Git repository archive:

  1. Resolve the revision that should become the archive.
    $ git rev-parse HEAD
    3608feb175138849f6e2b6fcb92a72ece92cc29c

    Replace HEAD with a release tag, branch name, or commit ID when the archive must come from a specific point in history.

  2. Create a tar archive with a top-level directory prefix.
    $ git archive --format=tar --prefix=project/ --output=project.tar HEAD

    Use a trailing slash in --prefix=project/ so extracted files land under one directory instead of directly in the destination folder.

  3. List the archive contents before sharing it.
    $ tar tf project.tar
    project/
    project/README.md
    project/src/
    project/src/app.py

    The listing should show the expected tracked files under the prefix. The .git directory and untracked working-tree files should be absent.

  4. Confirm the commit recorded in the tar archive when release traceability matters.
    $ git get-tar-commit-id < project.tar
    3608feb175138849f6e2b6fcb92a72ece92cc29c

    The value should match the revision resolved before the archive was created. For zip output, Git stores the commit ID as a file comment instead of a tar header.