Create a Github Repo from the Command Line

As a developer, a majority of my day is spent at my computer cranking away on various things. Inspired by a RubyRogues episode (Sharpening Tools with Ben Orenstein), I’ve taken an interest lately in making my day-to-day tasks more efficient, scratching the itches that are the minor annoyances in my development days. I thought I’d share the progress I have made in the hope that it will help others, so without further ado, I present: creating a Github repo from the command line.

The Command

Here is the straight-forward command line command to get the job done:

 curl -u "$username:$token" -d '{"name":"'$repo_name'"}'

To use, you could simply replace $username with your Github username, $token with a Personal Access Token for the same user (available for generation in your Github Settings > Applications), and $repo_name with your desired new Repository name.

The Bash Function

Creating a repo from the command line is definitely faster than going to Github and using the web app to get the job done, but in order to truly make this task speedy, we need some Bash programming. Thanks to coworkers Patrick and Brian for ideas here.

 github-create() {

 dir_name=`basename $(pwd)`

 if [ "$repo_name" = "" ]; then
 echo "Repo name (hit enter to use '$dir_name')?"
 read repo_name

 if [ "$repo_name" = "" ]; then

 username=`git config github.user`
 if [ "$username" = "" ]; then
 echo "Could not find username, run 'git config --global github.user <username>'"

 token=`git config github.token`
 if [ "$token" = "" ]; then
 echo "Could not find token, run 'git config --global github.token <token>'"

 if [ "$invalid_credentials" == "1" ]; then
 return 1

 echo -n "Creating Github repository '$repo_name' ..."
 curl -u "$username:$token" -d '{"name":"'$repo_name'"}' > /dev/null 2>&1
 echo " done."

 echo -n "Pushing local code to remote ..."
 git remote add origin$username/$repo_name.git > /dev/null 2>&1
 git push -u origin master > /dev/null 2>&1
 echo " done."

Plop this function into your ~/.bash_profile, open a new Terminal window or source ~/.bash_profile, and the function will be loaded up and ready for use.

Then while in an existing git project, running github-create will create the repo and push your master branch up in one shot. You will need to set some github config variables (instructions will be spit out if you don’t have them). Here’s an example:

 BASH:projects $ rails new my_new_project
 ..... (a whole lot of generated things)
BASH:projects $ cd my_new_project/
BASH:my_new_project $ git init && git add . && git commit -m 'Initial commit'
 ..... (a whole lot of git additions)
BASH:my_new_project $ github-create
 Repo name (hit enter to use 'my_new_project')?

 Creating Github repository 'my_new_project' ... done.
 Pushing local code to remote ... done.

Had I called the function with an argument — github-create my_project — then it would have used the argument and skipped the Repo name question.

I’d orginally whipped up something simpler and hard coded my username and access token into the function, but the provided example is a bit more robust. Plus it was a fun little exercise to experiment with Bash functions, not something I typically do, but am looking forward to doing more of as I tackle my next efficiency irks.

If you’ve done something similar, or have some comments on the implementation here, I’d love to hear about it in the comments below.

Eli uses his mathematics degree from Carnegie Mellon to blur the lines between the digital and physical worlds. He codes for Shure, Volunteers of America, and other clients from our Boulder, CO, office.

More posts by Eli