Git archive Command
In this page:
Exporting to Tar Archives
git archive bundles the files from a specific commit or branch into a single .tar file, but — unlike a plain copy of the directory — it includes nothing from .git/, so the output is a clean snapshot with no version history or repository metadata at all.
Example: Exporting to Tar Archives
git archive HEAD -o snapshot.tar
Exporting to Zip Archives
Passing --format=zip produces a .zip archive instead of a tar file, which is the more practical choice when you're handing a code snapshot to someone who doesn't have tar handy or isn't comfortable with a terminal at all, like a non-technical stakeholder.
Example: Exporting to Zip Archives
git archive --format=zip HEAD -o snapshot.zip
Archiving Specific Directories
You don't have to archive the whole repository — appending a path after the commit reference (e.g. git archive HEAD -- src/) limits the archive to just that directory, useful when only one module needs to be shared or deployed independently.
Example: Archiving Specific Directories
git archive HEAD -- src/ -o src.tar
Setting Compression Levels
For zip output, -0 through -9 control the tradeoff between how fast the archive is created and how small the resulting file is, with 0 skipping compression entirely and 9 compressing as much as possible at the cost of speed.
Example: Setting Compression Levels
git archive --format=zip -9 HEAD -o snapshot.zip
Adding Extracted Prefix Directories
By default, extracting the archive dumps every file directly into your current directory, which can clutter it or overwrite existing files. --prefix=myproject/ wraps everything inside a named folder in the archive, so extracting it creates that folder instead of scattering files loose.
Example: Adding Extracted Prefix Directories
git archive --prefix=myproject/ HEAD -o snapshot.tar
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: