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

Photo by Yancy Min on Unsplash

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

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

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

.github
/bin
/core
/languages
/scripts
/tests
.composer.json
my-plugin.php

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

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

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"
  }
}