Git archive

The git archive command allows you to combine multiple files into a single file, much like a zip utility. This means you can create an archive of your files, which can later be extracted to get individual files.



Tutorials dojo strip

Initial Example

To create a zip archive of the current revision:

$ git archive --format zip HEAD > archive-HEAD.zip

This command will create a zip archive named archive-HEAD.zip containing the files from the current revision.




ARCHIVE Options

OptionDescription
--format=<fmt>Specifies the format of the resulting archive. Possible values are tar, zip, tar.gz, tgz, and any custom format defined using the configuration option tar.<format>.command. If --format is not given and the output file is specified, the format is inferred from the filename (e.g., writing to foo.zip sets the format to zip). The default format is tar.
-l, --listShows all available formats.
-v, --verboseReports progress to stderr.
--prefix=<prefix>/Prepends the specified prefix to paths in the archive. The rightmost value is used for all tracked files.
-o <file>, --output=<file>Writes the archive to the specified file instead of stdout.
--add-file=<file>Adds a non-tracked file to the archive. Can be repeated to add multiple files. The path in the archive is built by concatenating the value of the last --prefix option (if any) and the basename of the file.
--add-virtual-file=<path>:<content>Adds the specified contents to the archive. Can be repeated to add multiple files. The path can be a C-style string with escape characters. This option creates a regular file with the specified path and content. Unlike --add-file, the path is not affected by the --prefix option.
--worktree-attributesLooks for attributes in .gitattributes files in the working tree as well.
--mtime=<time>Sets the modification time of archive entries. Without this option, the committer time is used if the input is a commit or tag, and the current time if it is a tree.
<extra>Specifies any additional options that the archiver backend understands.
--remote=<repo>Retrieves a tar archive from a remote repository instead of the local repository. The remote repository may place restrictions on which sha1 expressions are allowed.
--exec=<git-upload-archive>Used with --remote to specify the path to git-upload-archive on the remote side.
<tree-ish>Specifies the tree or commit to produce an archive for.
<path>If no path is specified, all files and subdirectories of the current working directory are included in the archive. If one or more paths are specified, only those are included.




Examples

1. Create a Tar Archive

To create a tar archive of the current revision:

$ git archive --format tar HEAD > archive-HEAD.tar

This will create a tar archive named archive-HEAD.tar.

2. Create an Archive of a Specific Branch

To create a zip archive of a specific branch, say develop:

$ git archive --format zip develop > develop-archive.zip

This will create a zip archive named develop-archive.zip containing the files from the develop branch.

3. Create an Archive with a Path Prefix

You can add a prefix to the files in the archive using the --prefix option. For example, to add a prefix project/ to the files in the archive:

$ git archive --format zip --prefix=project/ HEAD > project-archive.zip

This will create a zip archive named project-archive.zip, and all files in the archive will have the project/ prefix.

Scroll to Top