0
0
GitHow-ToBeginner · 3 min read

How to Use git archive: Create Archives from Git Repositories

Use git archive to create an archive file (like zip or tar) of files from a Git repository at a specific commit or branch. The command packages tracked files without including the Git history, useful for sharing or deployment.
📐

Syntax

The basic syntax of git archive is:

  • git archive [options] <ref> [<path>...]
  • <ref> is a commit, branch, or tag to archive from.
  • <path> is optional and limits files included.
  • --format sets archive type like zip or tar.
  • -o specifies the output archive file name.
bash
git archive --format=zip -o archive.zip main
💻

Example

This example creates a zip archive named project.zip from the main branch, including all tracked files.

bash
git archive --format=zip -o project.zip main
Output
Created archive file 'project.zip' containing files from the 'main' branch.
⚠️

Common Pitfalls

Common mistakes include:

  • Not specifying -o to save the archive to a file, which sends output to the terminal.
  • Trying to archive untracked or ignored files; git archive only includes tracked files.
  • Using an invalid branch or commit name in <ref>.
bash
git archive --format=tar main > archive.tar
# Wrong: output goes to terminal if -o not used

# Correct way:
git archive --format=tar -o archive.tar main
📊

Quick Reference

OptionDescription
--format=Set archive format: zip, tar, tar.gz, etc.
-o Write archive to specified file
Commit, branch, or tag to archive from
...Limit archive to specific files or folders
--prefix=/Add a directory prefix inside the archive

Key Takeaways

Use git archive to create compressed snapshots of tracked files without Git history.
Always specify the output file with -o to save the archive instead of printing to terminal.
You can archive from any commit, branch, or tag by specifying the reference.
git archive does not include untracked or ignored files.
Use --format to choose archive type like zip or tar.