If you’re designing in Sketch, you probably already know that the version 43 introduced a new file format. That's quite a thing and there’ve been written tons of posts describing what that makes possible.
Basically, each Sketch file is just a zipped bunch of JSONs and images with .sketch extension. This means there is a space for version control, because everything (except images) is represented in plaintext.
So if every Sketch file is just a ZIP, we only need to automate the unzipping and zipping process, right? Right. And fortunately, there are Git Hooks to help us.
- Set up a new repository for Sketch files
- Install Python 3 *
- Download the pre-commit, post-merge and post-checkout hooks from our GitHub repository
- Copy them into
.git/hooksfolder in your repository
- Make the scripts executable: run
chmod +x .git/hooks/pre-commit .git/hooks/post-merge .git/hooks/post-checkoutin your repository
- And try to commit some Sketch files!
* Actually, this is not necessary, but recommended, see the explanation below. The easiest way is to install Homebrew and then execute
brew install python3 .
How does it work?
There are three Git Hooks: the pre-commit hook is used for unzipping the file and committing changes and post-merge & post-checkout hook for pulling new changes and re-creating the Sketch file.
The pre-commit hook does three things:
- unzips all staged Sketch files,
- prettifies the JSONs so that we can use line-level versioning and
- adds the whole Sketch file-folder to the commit.
All you need is just to commit the Sketch file you want and the magic happens behind the scenes.
The second thing is especially important. Sketch uses minified JSONs (for obvious reasons), but that isn’t very handy for versioning. The reason why I recommend installing Python 3 is that the utility we're using for prettifying (json.tool) also sorts the keys alphabetically in Python 2 and I wanted to keep the Sketch files as much untouched as possible.
The post-merge hook is much simpler: it just zips the contents of the folder and creates back the Sketch file.
Post-checkout hook is exactly the same as post-merge hook and does the same thing when switching branches.
Hmm, so what?
Why all this? With having unzipped Sketch files, all the power of Git (and GitHub) comes. That means you can merge conflicts (working simultaneously on one Sketch file is no longer sci-fi), create pull requests, revert mistakes, …But you don’t have to: you can just enjoy safe, clear versioning.
This is just a first try and we’re beginning using this approach in Cloudaper right now, so I guess there will be further development, but we’d be happy about any feedback or even a pull request. Happy committing!
Update (November 21)
We've run into some minor issues, mainly with committing the Sketch file itself. So now the scripts works in the way it only backups the untouched Sketch files into special folder and never commits the work-in-progress Sketch file, which is recreated with each pull or branch switch. This should enable easier conflicts resolving.