How to package a theme into a zip file using git

Updated at: 16 April 2021

While coding a theme or a plugin we often need to copy files into a .zip archive in order to upload it to wordpress. What if one command could do it for us?

Everyone uses git, at least for backup. You can configure git in a way that would upload your files to the server every time you wrote git commit. But it isn’t as straight forward as someone might want. Often we only need to package a .zip archive that would let us upload it in the wordpress admin area.

You can do it with this one simple command:

git archive --format [format] --output [path/to/file] [branch name]

A ready command looks like this:

git archive --format zip --output C:\\Users\\{user_folder_name}\\Desktop\\{theme_or_plugin_name}.zip main

I have put mine in a npm script:

"zip": "git archive --format zip --output C:\\Users\\micha\\Desktop\\ main"

Now the only thing I have to do is write npm run zip in the command line and almost instantly I will have a .zip file with my theme on my desktop, ready to be uploaded. The git archive command respects the .gitignore file, so you can easily choose what files/folders you want in your .zip file.

Autor: Michał KuczekMichał Kuczek

Founder of Biiird Studio, UX designer, business philosopher, psychologist, and conflict mediator.