DEV Community

Cover image for Make A Custom .zip of your code using Git Archive & .gitattributes
markcummins
markcummins

Posted on

Make A Custom .zip of your code using Git Archive & .gitattributes

The Problem

So I have this WordPress plugin developed, and I'm ready to submit it to for review. So I have to make a .zip file of the folder. So I right-click on the folder and click 'Compress' right?

Well yes that works, but there's a bunch of stuff in that folder that I don't want to submit. There's a .git, .github, /bin folder and a bunch of stuff that the plugin doesn't need. Here's how the directory looks right now:

.github
/bin
/core
/languages
/node_modules
/scripts
/tests
/vendor
.gitignore
.composer.json
plugin.php
Enter fullscreen mode Exit fullscreen mode

Solution #1 - Git Archive

So I turn yet again to Google, and it provides a pretty good answer. git archive.

It takes all the files that are committed to the master branch and creates a .zip file of my plugin for me:

git archive --format zip --output \"plugin.zip\" master -0
Enter fullscreen mode Exit fullscreen mode

Here's how my 'plugin.zip' file looks:

.github
/bin
/core
/languages
/scripts
/tests
.composer.json
my-plugin.php
Enter fullscreen mode Exit fullscreen mode

Solution #1.0.1 - .gitattributes

There's another problem, here's how my .zip file looks. It excluded everything that was in my .gitignore file from the .zip, but I really don't want to submit the /tests folder or the /bin folder. So back to Google I go, and the answer is to create a .gitattributes file in the root of my directory.

.gitignore export-ignore
.gitattributes export-ignore
.github export-ignore
bin export-ignore
tests export-ignore
composer.json export-ignore
composer.lock export-ignore
Enter fullscreen mode Exit fullscreen mode

Note: Two things got me here. No. 1, you need to commit the .gitattributes file before running the git archive command, otherwise it wont work... and No .2, adding a / or ./ before a folder name wont work... (e.g. ./bin export-ignore)

So last time, I rerun my git archive command and voila, super clean:

/core
/languages
/scripts
my-plugin.php
Enter fullscreen mode Exit fullscreen mode

Next steps

It would be awesome to have this work automatically when you push it to GitHub. I imagine you could set up a GitHub action that would run some tests and then create a zip file as a release.


Columbo looking like the big eejit he is

One last thing...

Because we're lazy and we don't like remembering stuff, we can add the archive command to our .package.json. Now we just have to run npm run archive and we're good to go.

{
  "name": "...",
  "version": "...",
  "scripts": {
    "archive": "git archive --format zip --output \"mandrill-mail.zip\" master -0"
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
tbroyer profile image
Thomas Broyer • Edited

Note you can already download your repo as a zip file: docs.github.com/en/rest/repos/cont...

GitHub also automatically creates ZIP and TAR archives of each release.

I assume this uses git archive under the hood but maybe not 🤷‍♂️

Collapse
 
markcummins profile image
markcummins

Hi Thomas, absolutely you can download the whole repository as a .zip file, and you can also create releases and download those too. They're definitely great options to have, but they do create a .zip file of the whole repo I think.

In my case I did need to exclude some files that were in the repo before making the zip. It's probably not an Issue you'll run into every day, but still kinda cool to know that you can do it at the same time.

Collapse
 
tbroyer profile image
Thomas Broyer

It was in reaction to your "next steps": if GitHub relies on git archive, then your .gitattributes would be taken into account; and so you wouldn't need to setup an action to build that zip and attach it to the release: it'd come for free.

Thread Thread
 
markcummins profile image
markcummins

Oh yes I get it now. Thats awesome, thanks.