git-archive - Linux


Overview

git-archive is a Git command used to create an archive based on the current state of a repository. It allows you to create tar, zip, or tar.gz archives of your project or specific files at a particular commit or range of commits.

Syntax

git archive [options] [rev-list] [--] [paths...]

Parameters:

  • rev-list: Specifies the commit or commit range to create the archive from. Defaults to HEAD.
  • paths: (Optional) Specific files or directories to include in the archive. Defaults to the entire repository.

Options/Flags

  • -f, –format: Specify the format of the archive (tar, zip, tgz). Default: tar
  • -o, –output: Output file to save the archive. Defaults to standard output.
  • -v, –verbose: Provide detailed progress information.
  • -w, –worktree-attributes: Include worktree attributes in the archive.
  • -0, –null: Write filenames and directory names using NUL characters as separators.
  • –prefix: Specify a prefix to use for filenames within the archive.
  • –[no-]decode: Decode paths and filenames from Git internal encoding. Default: off

Examples

Create a tar archive of the entire repository:

git archive -o project.tar

Create a zip archive of specific files at commit SHA:

git archive -f zip -o project.zip SHA1 path/to/file1 path/to/file2

Create a tgz archive with a prefix and worktree attributes:

git archive -f tgz -o project.tgz --prefix=myproject --worktree-attributes

Common Issues

  • Permission denied: Ensure you have write permissions to the output directory specified with --output.
  • File not found: Check that the specified paths or commit range are valid.
  • Invalid format: Verify that the specified archive format (tar, zip, tgz) is supported.

Integration

Combine with git-filter-repo: Use git-filter-repo to manipulate the repository before creating the archive, such as removing sensitive data.

Automate with CI/CD: Create a script using git-archive to automatically create and upload archives to a repository or server.

Related Commands

  • git-clone: Clones a remote repository to a local directory.
  • git-tag: Creates or manages tags for specific commits.
  • git-bundle: Creates a bundle of commits that can be shared with other users.