← Back to Git Course | Chapter 6: Advanced Commands | Lesson 6 of 7

Git archive Command

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

bash
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

bash
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

bash
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

bash
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

bash
git archive --prefix=myproject/ HEAD -o snapshot.tar
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.