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.--formatsets archive type likeziportar.-ospecifies 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
-oto save the archive to a file, which sends output to the terminal. - Trying to archive untracked or ignored files;
git archiveonly 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 mainQuick Reference
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.