Git Archive: Export Your Code Without .git History
Use git archive to create a clean tarball or zip of your project without the .git directory. Perfect for releases and code distribution.
git archive --format=tar.gz --output=project.tar.gz HEADDetailed Explanation
What Does git archive Do?
git archive creates a compressed archive (tar, tar.gz, or zip) of your repository at a specific commit, tag, or branch. Crucially, the archive does not include the .git directory, making it ideal for distributing source code or deploying to servers.
Basic Usage
# Create a tar.gz of the current HEAD
git archive --format=tar.gz --output=project.tar.gz HEAD
# Create a zip archive
git archive --format=zip --output=project.zip HEAD
# Archive a specific tag
git archive --format=tar.gz --output=release-v1.0.tar.gz v1.0.0
Archiving a Subdirectory
# Archive only the src/ directory
git archive --format=tar.gz --output=src-only.tar.gz HEAD src/
Adding a Prefix
Add a top-level directory name to the archive contents:
git archive --format=tar.gz --prefix=my-project/ --output=release.tar.gz v1.0.0
This ensures files extract into a my-project/ directory rather than the current directory.
Piping to Remote
# Extract directly on a remote server via SSH
git archive HEAD | ssh deploy@server "tar -xf - -C /var/www/app"
Using .gitattributes for Export
You can exclude files from archives using .gitattributes:
# .gitattributes
tests/ export-ignore
.github/ export-ignore
.gitignore export-ignore
README.md export-ignore
Files marked export-ignore will be omitted from any git archive output.
Archive vs. Clone
git archive |
git clone |
|
|---|---|---|
| Includes .git | No | Yes |
| Includes history | No | Yes |
| Size | Minimal | Full repo |
| Use case | Distribution, deployment | Development |
Git archive is the cleanest way to package your code for distribution, ensuring no internal Git data or development files leak into the release.
Use Case
A team needs to deliver source code to a client who does not use Git. They use git archive to create a clean zip file containing only the production code at the tagged release.