I’m working on a new Pluralsight course at the moment, and one of the things I need to do is provide “before” and “after” code samples of all the demos I create. In theory should be easy since I’m using git for source control, so I can go back to any previous point in time using
git checkout and then zip up my working folder. But the trouble with that approach is that my zip would also contain a whole load of temporary files and build artefacts that I don’t want. What I needed was to be able to quickly create a zip file containing only the files under source control for a specific revision.
I thought at first I’d need to write my own tool to do this, but I discovered that the built-in
git archive command does exactly what I want. To create a zip file for a specific commit you just use the following command:
git archive --format zip --output example.zip 47636c1
Or if like me you are using tags to identify the commits you want to export, you can use the tag name instead of the hash:
git archive --format zip --output begin-demo.zip begin-demo
Or if you just want to export the latest files under source control, you can use a branch name instead:
git archive --format zip --output master.zip master
In fact, if you have Atlassian’s excellent free SourceTree utility installed, it’s even easier, since you can just right-click any commit and select “archive”. Anyway, this feature saved me a lot of time, so hope this proves useful to someone else as well.