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.
git archive Syntax:
Section titled “git archive Syntax:”git archive [options] <tree> [[--] <path>...]Options:
Section titled “Options:”| Option | Description |
|---|---|
| —format= | Format of the resulting archive: tar, zip, tgz, tar.gz |
| -l | List supported archive formats |
| -v | Be verbose |
| —prefix= | Prepend |
| —output= | Write the archive to |
| —worktree-attributes | Look for attributes in .gitattributes in worktree |
| —remote= | Archive from remote repository |
| —exec= | Use cmd to create the archive |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
| tree | Tree or commit to produce an archive for |
| path | If given, only these directories/files from the archive |
git archive Command Samples:
Section titled “git archive Command Samples:”Create a ZIP archive of the current HEAD
Section titled “Create a ZIP archive of the current HEAD”git archive --format=zip --output=repo.zip HEADCreates 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”git archive --format=tar --prefix=project-1.0/ HEAD > project-1.0.tarCreates a TAR archive with files prefixed for organized extraction.
Archive specific files or directories
Section titled “Archive specific files or directories”git archive --format=zip HEAD -- src/ docs/Archives only the src/ and docs/ directories from HEAD.
Archive from a specific commit or tag
Section titled “Archive from a specific commit or tag”git archive --output=v1.0.zip v1.0Creates an archive from the v1.0 tag.
Output to stdout
Section titled “Output to stdout”git archive --format=tar HEAD | gzip > archive.tar.gzPipes 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:
git archive --format=zip --output=repository.zip HEADHow 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:
git archive --format=tar --prefix=myproject/ HEAD > myproject.tarHow 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:
git archive --format=zip HEAD -- directory1/ file.txtHow 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:
git archive --remote=git://example.com/repo.git HEADHow 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:
git archive --format=tar.gz --output=archive.tar.gz HEADApplications of the git archive command
Section titled “Applications of the git archive command”- Creating ZIP or TAR archives of repository snapshots
- Generating release packages with version-specific prefixes
- Exporting only specific directories or files from the repository
- Archiving tagged releases for distribution
- Creating compressed archives for backup purposes
- Producing archives without Git metadata for deployment