Skip to content

Git Archive Tutorial

Overview

The git archive command is used to create an archive of files from a Git repository. This can be useful for exporting the contents of a repository, creating snapshots of specific commits, or preparing releases. The archive is typically created in formats like .tar, .zip, or .tar.gz.

What is git archive

git archive is a Git command that creates an archive of files from a Git repository. It allows you to export the contents of the repository as a compressed file (e.g., .zip, .tar.gz) at a specific commit, branch, or tag.

Basic Syntax

git archive [options] <commit> --output=<archive-file>
  • <commit>: The commit, branch, or tag to archive.
  • --output=<archive-file>: The name of the output archive file.

Creating Archives

Archive Format

You can specify the format of the archive using the --format option. Common formats include tar, zip, and tar.gz.

Archive Specific Commit

To create an archive of a specific commit:

git archive --format=zip --output=archive.zip <commit>

Archive a Branch

To create an archive of the latest commit on a branch:

git archive --format=tar --output=branch.tar <branch-name>

Archive a Tag

To create an archive of a specific tag:

git archive --format=tar.gz --output=tag.tar.gz <tag-name>

Examples of Using git archive

Example 1: Creating a ZIP Archive of the Latest Commit

To create a ZIP archive of the latest commit on the main branch:

git archive --format=zip --output=main-latest.zip main

Example 2: Creating a TAR Archive of a Specific Tag

To create a TAR archive of a tag named v1.0.0:

git archive --format=tar --output=v1.0.0.tar v1.0.0

Example 3: Creating a GZipped TAR Archive of a Branch

To create a gzipped TAR archive of the develop branch:

git archive --format=tar.gz --output=develop.tar.gz develop

Example 4: Creating an Archive and Compressing it with Gzip

To create a TAR archive and compress it with Gzip:

git archive --format=tar <branch-or-tag> | gzip > archive.tar.gz

Common Use Cases

  • Releasing Software: Create a snapshot of your repository at a specific commit, tag, or branch for distribution.
  • Backup: Archive the current state of a repository for backup purposes.
  • Exporting Code: Export a portion of the repository's code without including the entire Git history.

Summary

The git archive command is a versatile tool for creating archives of Git repository contents. It allows you to export files at specific commits, branches, or tags in various formats, such as .zip, .tar, and .tar.gz. This can be useful for software releases, backups, or exporting code.