Cloning all of your Github Repositories

Recently I got my new dual boot machine running. This meant that I wanted to clone all of my repositories from Github onto my local computer. I could have easily just done it manually one at a time, but decided I would put my python skills to work.

#!/usr/bin/python

import os  
import urllib2  
import json  
import sys  
import time  
import subprocess  
from subprocess import call

class bcolors:  
    PULLED = '\033[94m'
    EXISTS = '\033[92m'
    DOESNTEXIST = '\033[93m'
    END = '\033[0m'

# Set Github Key variable to the Environment Variable named "GITHUB_KEY"
githubKey = os.environ['GITHUB_KEY']

# Call the repos endpoint from github, get the user's repos, parse this into JSON
request = urllib2.Request("https://api.github.com/user/repos", headers={"Authorization" : "token " + githubKey})  
contents = urllib2.urlopen(request).read()  
reposJson = json.loads(contents)

# Iterate all of the repos the user owns
for repo in reposJson:  
    repoName = repo['name']

    # Determine if the repo already exists
    if os.path.isdir(repoName):
        # Repo exists, print it out in green.
        print bcolors.EXISTS + repoName + bcolors.END,
        sys.stdout.flush()
    else:
        # Repo doesn't exist, print the name in orange
        print bcolors.DOESNTEXIST + repoName + bcolors.END,
        sys.stdout.flush()

        # clone the repo using ssh and no output
        call (["git", "clone", repo['git_url']], stdout=open(os.devnull, "w"), stderr=subprocess.STDOUT)

        # Print the repo in blue when it is finished pulling
        print "\r" + bcolors.PULLED + repoName + bcolors.END,
        sys.stdout.flush()

    # Wait a second to make it look good
    time.sleep(1)
    print

This code is available as a gist on Github

This script is really simple. Set an environment variable named GITHUB_KEY to be your Github Access Token. Then all you need to do is run this script and it will clone all of your repositories to the present directory.

The script will only clone if the repository is not currently cloned to the current directory. If it is already cloned, the repository will be output in green. If it needs to be cloned, it will show as orange, and when the clone completes it will be blue.

I'm very happy with this script. Please use it if you end up migrating to a new dev environment and want to get all of your code quickly.