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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Daniel
  • 14,004
  • 16
  • 96
  • 156

3 Answers3

220

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

Abel Callejo
  • 13,779
  • 10
  • 69
  • 84
EncryptedWatermelon
  • 4,788
  • 1
  • 12
  • 28
  • 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 04 '19 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? – Abel Callejo Jan 17 '23 at 03:59
  • @AbelCallejo HEAD is your current branch vs specifying a branch to export – EncryptedWatermelon Jan 17 '23 at 19:56
  • 1
    `git archive` will infer format from the output file, reducing the command to `git archive -o project.zip master`. – young_souvlaki Feb 08 '23 at 03:47
11

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
Abel Callejo
  • 13,779
  • 10
  • 69
  • 84
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
Iceberg
  • 2,744
  • 19
  • 19
  • 1
    The biggest issue in this setup is that you also add the `gitignore`d files, and there might be some secrets in it. – strboul Feb 20 '23 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 06 '23 at 00:59