How do I export my project as a .zip of git repository?
Asked Answered
D

4

132

I was recently asked to export as a .zip file one of my projects on my Git repository.

I have actually never had to do this in the 4 years I have been using Git.

I would prefer an answer that is all done inside command line terminal.

Damle answered 4/4, 2019 at 12:8 Comment(0)
U
248

git archive --format=zip --output /full/path/to/zipfile.zip master

Unidirectional answered 4/4, 2019 at 12:12 Comment(5)
By the way, the command that I used was git archive --format=zip --output project.zip HEAD, but I still checked the answer off as it did bring me dramatically closer to the solution.Damle
I'm considering editing the answer with the HEAD parameter at the tail-end. What is the difference between having HEAD with not?Faefaeces
@AbelCallejo HEAD is your current branch vs specifying a branch to exportUnidirectional
git archive will infer format from the output file, reducing the command to git archive -o project.zip master.Bike
Could you change master to main for 2023 feel? @UnidirectionalAllaallah
F
19

Shorthand examples

# zip archive
git archive -o output.zip master
# tape archive
git archive -o output.tar master
# tarball
git archive -o output.tar.gz master

According to the official documentation, the -o option is capable of identifying the target compression format through the extension file name.

Any other unidentified format will be defaulted to the tape archive which is equivalent to:

--format=tar
Faefaeces answered 18/1, 2023 at 0:57 Comment(0)
W
5

Following code might help if you want to include .git/ too and don't want to include other extra files which haven't been tracked by git. Suppose the path of your git project is /opt/helloworld/, commit whatever are left, then you can do as the following:

git clone /opt/helloworld/  folder2
cd folder2
tar -czf helloworld-latest.zip folder2
Warga answered 27/12, 2021 at 3:36 Comment(2)
The biggest issue in this setup is that you also add the gitignored files, and there might be some secrets in it.Bandsman
Including .git usually isn’t a good idea, since it could have arbitrary leftover state in it (maybe secrets too?). You should use git bundle to transfer a repository in most cases.Leduc
B
1
  1. I personally prefer to create the archive with prefix for the project, otherwise the consumers of the archive will need to create take care of the scoping themselves. Please note that you will need the trailing forward slash for the project directory.

  2. I also prefer shortening as much as possible, so when you have the right suffix for the extension, git will recognise the format automatically.

  3. You can also just use -o instead of --output for convenience and rapid typing.

  4. Also, GitHub, etc, prefer to use main instead of master these days.

git archive --prefix foo/ -o ~/foo.zip main
Bryan answered 28/10, 2023 at 7:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.