Skip to content

archive Git Command Guide

The git archive command creates an archive (tar or zip format) of files and directories from a named tree in the Git repository. It allows exporting repository contents at a specific point in time without the .git directory or history.

Terminal window
git archive [options] <tree> [[--] <path>...]
OptionDescription
—format=Format of the resulting archive: tar, zip, tgz, tar.gz
-lList supported archive formats
-vBe verbose
—prefix=Prepend / to each filename in archive
—output=Write the archive to instead of stdout
—worktree-attributesLook for attributes in .gitattributes in worktree
—remote=Archive from remote repository
—exec=Use cmd to create the archive
ParameterDescription
treeTree or commit to produce an archive for
pathIf given, only these directories/files from the archive
Terminal window
git archive --format=zip --output=repo.zip HEAD

Creates a ZIP archive of all files at the current HEAD.

Create a TAR archive with prefix for unpacking

Section titled “Create a TAR archive with prefix for unpacking”
Terminal window
git archive --format=tar --prefix=project-1.0/ HEAD > project-1.0.tar

Creates a TAR archive with files prefixed for organized extraction.

Terminal window
git archive --format=zip HEAD -- src/ docs/

Archives only the src/ and docs/ directories from HEAD.

Terminal window
git archive --output=v1.0.zip v1.0

Creates an archive from the v1.0 tag.

Terminal window
git archive --format=tar HEAD | gzip > archive.tar.gz

Pipes the archive to stdout for further processing.

How do I create a ZIP archive of my Git repository?

Section titled “How do I create a ZIP archive of my Git repository?”

To create a ZIP archive of your Git repository, use the following command:

Terminal window
git archive --format=zip --output=repository.zip HEAD

How can I create an archive with a specific prefix for file paths?

Section titled “How can I create an archive with a specific prefix for file paths?”

To create an archive with a specific prefix appended to file paths, execute:

Terminal window
git archive --format=tar --prefix=myproject/ HEAD > myproject.tar

How do I archive only specific files or directories from the repository?

Section titled “How do I archive only specific files or directories from the repository?”

To archive only specific files or directories, use:

Terminal window
git archive --format=zip HEAD -- directory1/ file.txt

How can I archive from a remote Git repository?

Section titled “How can I archive from a remote Git repository?”

To archive from a remote repository, run:

Terminal window
git archive --remote=git://example.com/repo.git HEAD

How do I create a compressed TAR.GZ archive?

Section titled “How do I create a compressed TAR.GZ archive?”

To create a compressed TAR.GZ archive, use:

Terminal window
git archive --format=tar.gz --output=archive.tar.gz HEAD
  1. Creating ZIP or TAR archives of repository snapshots
  2. Generating release packages with version-specific prefixes
  3. Exporting only specific directories or files from the repository
  4. Archiving tagged releases for distribution
  5. Creating compressed archives for backup purposes
  6. Producing archives without Git metadata for deployment