Automatically Create GitHub Gists for your Medium Articles

The script! (if you’re just here for this)

This article provides you with an easy script to create GitHub Gists from your repositories.

Adding code in Medium articles can be done in one of three ways. In line code like this, in a code block

or with a GitHub gist

Example of a GitHub Gist

Let us focus on those gists. When I wanted to add them into my Medium articles, I knew they were hosted on GitHub. So naturally, the first thing I did was create a GitHub repository for my Medium articles and start adding snippet files. Each file was going to be a single gists. When I was done creating ten-ish files, I though to myself “Okay, now how do I turn theses into gists?”. Aaaaaand that’s when I went “Urgh… 😑”. It turns out that GitHub Gists is something different to GitHub Repositories. In order to create gists, I had to manually create each gists in the browser. But I already spent all that time creating and maintaining them as files in my repository. Moreover, Gists does not provide any structure to sets of gists related to separated projects. They’re all just thrown on the same heap of gists. There had to be a better option. And that’s when I found the GitHub CLI, which allows you to create a gist from the command line with

After installing the CLI and authenticating my GitHub account, the only thing left was to automate this process to create gists from all my files and not having to execute this CLI command for each file. A super basic Python script did the trick. It takes a directory as an input argument and will create gists from all the files within that directory.

Simple Python script to create gists of all files in a directory

I just run the script. (Yes I did use it for this article as well). And done, it has created all my gists.

Example of script output

Those links can directly be embedded in your Medium article. Just copy and paste the link and hit enter.

This process can still be improved by adding an update gists script, because the current script will each time create new gists, even though a gist of a file already exists.

Hope it helped you in some way or another!

Self-taught software, data, and cloud engineer with a PhD in Astrophysics who continuously improves his skills through blogs, videos, and online tutorials.