How to Make a Zip of a Repository’s Contents with git archive
Running git archive --format=zip --output=<filename>.zip HEAD packs only the files in the current commit into a zip file. It is useful when you want to bundle source code for distribution, or when you are asked to send a zip without the .git folder or node_modules. The .git folder is left out, and so are uncommitted changes and files ignored by .gitignore. I ran the commands in a test environment and include the output before and after each step as it was.
The test environment was Ubuntu 24.04 with Git 2.43.0, and I used UnZip 6.00 to check the contents of the zip. I set the author name and email to example values (Neko <neko@example.com>) and fixed the commit dates with environment variables. The zip files were created one level above the repository (../). I did not test on Windows or macOS, using --remote to fetch from another server, or the detailed differences in tar format.
- 1. The repository before zipping
- 2. Creating a zip with git archive
- 3. Without –format, the file extension decides the format
- 4. Naming the folder when extracting, with –prefix
- 5. Making a zip of an older version by specifying a tag or branch
- 6. Zipping only part of the tree
- 7. Excluding files you do not distribute, with export-ignore
- 8. Adding files Git does not track, with –add-file
- 9. When you also want uncommitted changes
The repository before zipping
I prepared a repository called neko-app. It has two commits, and the first commit is tagged v1.0. In the working folder I then placed an uncommitted change (a rewrite of src/main.js), a file that Git does not track (memo.txt), and items ignored by .gitignore (node_modules/ and .env).
$ git status --short --ignored
M src/main.js
?? memo.txt
!! .env
!! node_modules/
$ git log --oneline
0c66e81 鳴き声を2回にする
c08a37c 最初のコミット
$ git ls-files
.gitignore
README.md
docs/guide.md
src/main.js
tests/main.test.js
The five files listed by git ls-files are the ones Git tracks.
Creating a zip with git archive
Run the following command in the repository folder. According to the official gitrevisions reference, HEAD points to the commit that the current work is based on.
$ git archive --format=zip --output=../neko-app.zip HEAD
It finishes without printing anything, and ../neko-app.zip is created. I listed the contents with unzip -l.
$ unzip -l ../neko-app.zip
Archive: ../neko-app.zip
0c66e81f6b6c4a3b88a7aea7c4bcd9793a83c638
Length Date Time Name
--------- ---------- ----- ----
19 2026-11-12 10:30 .gitignore
21 2026-11-12 10:30 README.md
0 2026-11-12 10:30 docs/
12 2026-11-12 10:30 docs/guide.md
0 2026-11-12 10:30 src/
35 2026-11-12 10:30 src/main.js
0 2026-11-12 10:30 tests/
5 2026-11-12 10:30 tests/main.test.js
--------- -------
92 8 files
$ unzip -p ../neko-app.zip src/main.js
console.log("にゃーにゃー");
Only the five files from git ls-files and their folders went in. The .git folder, memo.txt, node_modules/, and .env are not included. The contents of src/main.js are the "meow meow" from when it was committed. On the other hand, .gitignore went into the zip because it is a committed file.
git archive packs the files recorded in the commit you specify, not the current state of the working folder. Uncommitted changes do not go into the zip.
After creating the zip, the working folder is unchanged.
$ git status --short --ignored
M src/main.js
?? memo.txt
!! .env
!! node_modules/
File dates and the commit ID kept in the zip
Every file in the listing has the time 10:30. That is the date of the HEAD commit. The 0c66e81… line above the listing is the zip file's comment. Extracting it with unzip -z gave the same commit ID that git rev-parse HEAD returned.
$ unzip -z ../neko-app.zip
Archive: ../neko-app.zip
0c66e81f6b6c4a3b88a7aea7c4bcd9793a83c638
$ git rev-parse HEAD
0c66e81f6b6c4a3b88a7aea7c4bcd9793a83c638
The official git archive reference also says that when a commit or tag is specified, the file dates are taken from that commit, and the commit ID is written into the zip's comment. This lets you check later which commit the zip was made from.
Without –format, the file extension decides the format
When the --output file (short form -o) ends in .zip, the result is a zip even without --format=zip. Using .tar produces a tar archive, and writing to standard output without -o also produced a tar archive.
$ git archive --list
tar
tgz
tar.gz
zip
$ git archive -o ../auto.zip HEAD
$ file ../auto.zip
../auto.zip: Zip archive data, at least v1.0 to extract, compression method=store
$ git archive -o ../auto.tar HEAD
$ file ../auto.tar
../auto.tar: POSIX tar archive
$ git archive HEAD > ../stdout.out
$ file ../stdout.out
../stdout.out: POSIX tar archive
The official description of –format says that when --format is omitted and the output file name tells the format, the name decides it, and otherwise tar is used. When I redirected output into a file named .zip, the contents were still tar. To get a zip in that case, add --format=zip.
$ git archive HEAD > ../redirect.zip
$ file ../redirect.zip
../redirect.zip: POSIX tar archive
$ git archive --format=zip HEAD > ../redirect2.zip
$ file ../redirect2.zip
../redirect2.zip: Zip archive data, at least v1.0 to extract, compression method=store
Naming the folder when extracting, with –prefix
The zip made above extracts its files directly into the current folder. Adding --prefix=neko-app/ puts neko-app/ in front of every file, so they all end up in one folder.
$ git archive -o ../neko-app-prefix.zip --prefix=neko-app/ HEAD
$ unzip -l ../neko-app-prefix.zip
Archive: ../neko-app-prefix.zip
0c66e81f6b6c4a3b88a7aea7c4bcd9793a83c638
Length Date Time Name
--------- ---------- ----- ----
0 2026-11-12 10:30 neko-app/
19 2026-11-12 10:30 neko-app/.gitignore
21 2026-11-12 10:30 neko-app/README.md
0 2026-11-12 10:30 neko-app/docs/
12 2026-11-12 10:30 neko-app/docs/guide.md
0 2026-11-12 10:30 neko-app/src/
35 2026-11-12 10:30 neko-app/src/main.js
0 2026-11-12 10:30 neko-app/tests/
5 2026-11-12 10:30 neko-app/tests/main.test.js
--------- -------
92 9 files
$ unzip -q ../neko-app-prefix.zip -d ../extracted && ls -A ../extracted ../extracted/neko-app
../extracted:
neko-app
../extracted/neko-app:
.gitignore
README.md
docs
src
tests
Inside the extraction folder extracted, one neko-app folder was created, and the files were inside it.
The trailing / cannot be omitted. With --prefix=neko-app, no folder was made, and neko-app was simply attached to the front of each file name. (unzip -Z1 lists just the file names, one per line.)
$ git archive -o ../no-slash.zip --prefix=neko-app HEAD
$ unzip -Z1 ../no-slash.zip
neko-app.gitignore
neko-appREADME.md
neko-appdocs/
neko-appdocs/guide.md
neko-appsrc/
neko-appsrc/main.js
neko-apptests/
neko-apptests/main.test.js
Making a zip of an older version by specifying a tag or branch
Instead of HEAD, you can write a tag name, branch name, or commit ID to zip the contents as of that point. I specified the v1.0 tag and included the version in the folder name too.
$ git archive -o ../neko-app-v1.0.zip --prefix=neko-app-v1.0/ v1.0
$ unzip -p ../neko-app-v1.0.zip neko-app-v1.0/src/main.js
console.log("にゃー");
$ unzip -z ../neko-app-v1.0.zip
Archive: ../neko-app-v1.0.zip
c08a37c1b28afa06691e216e4841f126208b35e4
$ git rev-parse v1.0^{commit}
c08a37c1b28afa06691e216e4841f126208b35e4
src/main.js has the "meow" from the first commit, and the comment holds the ID of the commit that v1.0 points to. You can get an older version without switching the branch you are on.
Zipping only part of the tree
If you write a folder or file name after the commit, only that part goes into the zip.
$ git archive -o ../docs.zip HEAD docs
$ unzip -l ../docs.zip
Archive: ../docs.zip
0c66e81f6b6c4a3b88a7aea7c4bcd9793a83c638
Length Date Time Name
--------- ---------- ----- ----
0 2026-11-12 10:30 docs/
12 2026-11-12 10:30 docs/guide.md
--------- -------
12 2 files
Inside the zip, the files are also placed under docs/. If you want only the contents without the docs/ folder, you can use the form that joins the commit and the path with :, as in HEAD:docs.
$ git archive -o ../docs-tree.zip HEAD:docs
$ unzip -l ../docs-tree.zip
Archive: ../docs-tree.zip
Length Date Time Name
--------- ---------- ----- ----
12 2026-11-12 11:19 guide.md
--------- -------
12 1 file
$ unzip -z ../docs-tree.zip
Archive: ../docs-tree.zip
guide.md went in at the top level, but its date was the time the command ran, not the commit's 10:30. The zip comment is also empty. HEAD:docs points to a folder (a tree in Git terms), not a commit, so, as the official reference explains, the current time is used and no commit ID is recorded. If you want the zip to record which commit it came from, use the HEAD docs form.
Excluding files you do not distribute, with export-ignore
Files such as test folders or config files that you want to keep in Git but leave out of the zip can be excluded by writing export-ignore in .gitattributes (official gitattributes reference). I wrote lines that exclude tests/, .gitignore, and .gitattributes itself.
$ printf "tests/ export-ignore\n.gitattributes export-ignore\n.gitignore export-ignore\n" > .gitattributes
$ git archive -o ../before-commit.zip HEAD
$ unzip -l ../before-commit.zip
Archive: ../before-commit.zip
0c66e81f6b6c4a3b88a7aea7c4bcd9793a83c638
Length Date Time Name
--------- ---------- ----- ----
19 2026-11-12 10:30 .gitignore
21 2026-11-12 10:30 README.md
0 2026-11-12 10:30 docs/
12 2026-11-12 10:30 docs/guide.md
0 2026-11-12 10:30 src/
35 2026-11-12 10:30 src/main.js
0 2026-11-12 10:30 tests/
5 2026-11-12 10:30 tests/main.test.js
--------- -------
92 8 files
Creating .gitattributes alone was not enough. tests/ and .gitignore were still in the zip. This is because git archive reads the .gitattributes in the commit being zipped. To use a .gitattributes that has not been committed yet, add --worktree-attributes.
$ git archive --worktree-attributes -o ../worktree-attr.zip HEAD
$ unzip -l ../worktree-attr.zip
Archive: ../worktree-attr.zip
0c66e81f6b6c4a3b88a7aea7c4bcd9793a83c638
Length Date Time Name
--------- ---------- ----- ----
21 2026-11-12 10:30 README.md
0 2026-11-12 10:30 docs/
12 2026-11-12 10:30 docs/guide.md
0 2026-11-12 10:30 src/
35 2026-11-12 10:30 src/main.js
--------- -------
68 5 files
So that you do not have to pass this option every time, I committed .gitattributes. This commit is dated 11:00. After that, the same result came out without any options.
$ git add .gitattributes && git commit -q -m "配布用のzipにテストを入れない"
$ git archive -o ../release.zip HEAD
$ unzip -l ../release.zip
Archive: ../release.zip
6565d678b3d1edb455dd3cabfa3d8f4a7df7567e
Length Date Time Name
--------- ---------- ----- ----
21 2026-11-12 11:00 README.md
0 2026-11-12 11:00 docs/
12 2026-11-12 11:00 docs/guide.md
0 2026-11-12 11:00 src/
35 2026-11-12 11:00 src/main.js
--------- -------
68 5 files
The official ATTRIBUTES section also says that attributes are normally read from the .gitattributes in the commit being zipped, and that --worktree-attributes makes it read the .gitattributes in the working folder instead.
Adding files Git does not track, with –add-file
--add-file adds a file that Git does not track to the zip. I added memo.txt.
$ git archive -o ../with-memo.zip --add-file=memo.txt HEAD
$ unzip -l ../with-memo.zip
Archive: ../with-memo.zip
6565d678b3d1edb455dd3cabfa3d8f4a7df7567e
Length Date Time Name
--------- ---------- ----- ----
21 2026-11-12 11:00 README.md
0 2026-11-12 11:00 docs/
12 2026-11-12 11:00 docs/guide.md
0 2026-11-12 11:00 src/
35 2026-11-12 11:00 src/main.js
7 2026-11-12 11:00 memo.txt
--------- -------
75 6 files
According to the official –add-file description, the location inside the zip is the --prefix you gave before it, joined with the file name (the name without its folders). When I added docs/memo.txt together with --prefix=neko-app/, it went into the zip as neko-app/memo.txt, not neko-app/docs/memo.txt.
$ cp memo.txt docs/memo.txt && git archive -o ../with-docs-memo.zip --prefix=neko-app/ --add-file=docs/memo.txt HEAD && rm docs/memo.txt
$ unzip -Z1 ../with-docs-memo.zip
neko-app/
neko-app/README.md
neko-app/docs/
neko-app/docs/guide.md
neko-app/src/
neko-app/src/main.js
neko-app/memo.txt
I copied memo.txt into docs/, created the zip, and then deleted the copy.
When you also want uncommitted changes
If you want the zip to include your in-progress changes, the reliable approach is to commit first and then create the zip. If you want to avoid committing, you can use git stash create to turn the working changes into a temporary commit and pass its ID to git archive.
$ git status --short
M src/main.js
?? memo.txt
$ git archive -o ../wip.zip $(git stash create)
$ git stash list
$ unzip -l ../wip.zip
Archive: ../wip.zip
70ecf49640520a046c4cb0c8d22e035222f4e27a
Length Date Time Name
--------- ---------- ----- ----
21 2026-11-12 11:00 README.md
0 2026-11-12 11:00 docs/
12 2026-11-12 11:00 docs/guide.md
0 2026-11-12 11:00 src/
26 2026-11-12 11:00 src/main.js
--------- -------
59 5 files
$ unzip -p ../wip.zip src/main.js
console.log("作業中");
$ git status --short
M src/main.js
?? memo.txt
src/main.js went into the zip with the in-progress content. git stash list stayed empty, and the changes in the working folder were kept as they were. However, memo.txt, which Git does not track, was not included this way. The official git stash reference says that git stash create is meant for use in scripts and is not a command you normally use.





