109

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.

0

3 Answers 3

220

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

4
  • 24
    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.
    – Daniel
    Apr 4, 2019 at 12:25
  • I'm considering editing the answer with the HEAD parameter at the tail-end. What is the difference between having HEAD with not? Jan 17 at 3:59
  • @AbelCallejo HEAD is your current branch vs specifying a branch to export Jan 17 at 19:56
  • 1
    git archive will infer format from the output file, reducing the command to git archive -o project.zip master. Feb 8 at 3:47
12

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
3

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
2
  • 1
    The biggest issue in this setup is that you also add the gitignored files, and there might be some secrets in it.
    – strboul
    Feb 20 at 21:05
  • 2
    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.
    – Ry-
    May 6 at 0:59

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.