Skip to content

Instantly share code, notes, and snippets.

Last active March 1, 2024 06:07
Show Gist options
  • Save mbostock/3883098 to your computer and use it in GitHub Desktop.
Save mbostock/3883098 to your computer and use it in GitHub Desktop.
The Gist to Clone All Gists
license: gpl-3.0

Run like so:

chmod u+x gist-clone-all
./gist-clone-all username OAUTH_TOKEN

You'll want to replace “username” with your own username and “OAUTH_TOKEN” with an OAuth token.

This script clones using the push URL, so you should probably be the owner of the gists. You could also use this to clone someone else's gists, but in that case you may wish to edit the code to use gist_pull_url instead.

#!/usr/bin/env node
var fs = require("fs"),
https = require("https"),
exec = require("child_process").exec;
// TODO --pull or --push
var user = process.argv[2],
token = process.argv[3];
fetchAndClone(1, function callback(error, nextPage) {
if (error) throw error;
if (nextPage > 0) fetchAndClone(nextPage, callback);
function fetchAndClone(page, callback) {
fetch(page, function(error, gists) {
if (error) return callback(error);
if (gists.length) ++page; else page = -1;
function cloneNext(gist) {
if (!gist) return callback(null, page);
if (directoryExists( return cloneNext(gists.pop());
console.log("cloning " +;
exec("git clone" + + ".git", function(error, stdout, stderr) {
if (error) return callback(error);
function fetch(page, callback) {
var request = https.request({
hostname: "",
port: 443,
path: "/users/" + encodeURIComponent(user) + "/gists?page=" + page,
method: "GET",
headers: {
"Accept": "application/vnd.github.v3+json",
"Authorization": "token " + token,
"User-Agent": "mbostock/gist-clone-all"
}, function(response) {
var chunks = [];
response.on("data", function(chunk) { chunks.push(chunk); });
response.on("end", function() { callback(null, JSON.parse(chunks.join(""))); });
request.on("error", callback);
function directoryExists(path) {
try {
return fs.lstatSync(path).isDirectory();
} catch (ignored) {
return false;
for i in *; do
if [ -d $i ]; then
pushd $i
git commit -am '' --allow-empty-message && git push
Copy link

lmj0011 commented Dec 30, 2016

I only had to set the gist permission for the OAuth token, but you may not even need that set.

correction: Only the "gist" scope is needed for the OAuth token.

Copy link

what is gist_pull_url?

Copy link

nlevick commented Mar 30, 2017

Great work! Thank you!

Copy link

I've been using but I was wondering if there was a way to clone all other people's forks of my gists, too, so I can easily see what changes have been made. I've been adding them manually with username as remote name.

Copy link

erbanku commented Mar 30, 2022

I have failed many times with this. but after searching the internet I found an easy one to do the same job, ghcloneall 1.10.1, enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment