Creating GitHub Repositories at the Command Line

I like programming in vim. But when I want to share a new project with the world, I have to fire up a web browser in order to create a new repository in GitHub.

No more. I dug into the GitHub API literature and found that you can create a repository by POST request to their RESTful web API. For example, you can  create a GitHub personal API token with at least the “public_repo” scope and then:

curl -u YOURGITHUBUSERNAME:YOURPERSONALAPITOKEN --header "Content-Type: application/json" --request POST --data '{"name": "test-repo"}' https://api.github.com/user/repos

See the API documentation link above for a full list of optional data that you can send to this endpoint. And of course, you can use any HTTP tool to send this request, curl is not required.

To make the process smoother, I added the following variables to my environment:

export GITHUB_CREATE_REPO_API_KEY="YOURPERSONALAPITOKEN"
export GITHUB_USERNAME="YOURGITHUBUSERNAME"

And then created the following shell script:

#!/bin/sh

curl -u $GITHUB_USERNAME:$GITHUB_CREATE_REPO_API_KEY --header "Content-Type: application/json" --request POST --data "{\"name\": \"$1\", \"description\": \"$2\"}" https://api.github.com/user/repos

With that script in my PATH, the command mkgithubrepo data-structures-exercises "Various data structures implemented in Python and C." creates a GitHub repository at https://github.com/JASchilz/data-structures-exercises with the description “Various data structures implemented in Python and C.”

Leave a Reply

Your email address will not be published. Required fields are marked *