bashbeginner

Git Archive and Export Project

Export clean project archives from Git without .git directory for deployment or distribution.

bash
# Create tar.gz archive from HEAD
git archive --format=tar.gz --prefix=project/ HEAD > project.tar.gz

# Create zip archive
git archive --format=zip HEAD > project.zip

# Archive a specific branch
git archive --format=tar.gz main > release.tar.gz

# Archive a specific tag
git archive --format=tar.gz v1.0.0 > v1.0.0.tar.gz

# Archive a subdirectory only
git archive HEAD src/lib/ > lib-only.tar.gz

# Archive with prefix (extracts to project/ folder)
git archive --prefix=myapp-v1.0/ v1.0.0 | gzip > myapp-v1.0.tar.gz

# Archive specific files
git archive HEAD -- package.json tsconfig.json src/ > partial.tar.gz

# Pipe directly to remote server
git archive HEAD | ssh deploy@server 'cd /opt/app && tar xf -'

# Export using .gitattributes (exclude files from archive)
# In .gitattributes:
# .gitignore export-ignore
# tests/ export-ignore
# .github/ export-ignore
# README.md export-ignore

# Verify export-ignore works
git archive HEAD | tar tf - | head -20

Use Cases

  • Creating clean deployment packages
  • Distributing source code without git history
  • Exporting specific versions for vendors

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.