Last Updated: November 19, 2020
·
72.08K
· metaraine

Create new github repo from command line

Note: While the below is a great way to do it yourself, hauleth suggested Hub which does all this and more!

Create a new shell script called "git-create"

#!/bin/sh

repo_name=$1
test -z $repo_name && echo "Repo name required." 1>&2 && exit 1

curl -u 'your_github_username' https://api.github.com/user/repos -d "{\"name\":\"$repo_name\"}"

As tchaudhri mentioned in the comments, don't forget to make the above script executable by running chmod +x git-create.

Make sure you save it somewhere on your PATH. If you don't have a directory for custom shell scripts on your PATH, you can save it in ~/bin and then add that to your PATH by editing ~/.bash_profile as follows:

PATH=$PATH:~/bin
export PATH

Now you can easily create a new github repo from with the following command:

git create mynewrepo

If you assume this command is run from a valid local git repo (or the folder containing what should become the repo) you can add the following lines to "git-create" to automatically add the remote:

git init
git remote add origin "https://github.com/your_github_username/$repo_name.git"

git init is safe and won't mess anything up if the local repo already exists.

Enjoy!

Again, Hub recommended by hauleth does all this and more!

Related protips:

git checkout remote branch

9 Responses
Add your response

Or you can use Hub which provide some more features.

over 1 year ago ·

Good to know! Thanks hauleth!

over 1 year ago ·

As already stated, Hub is a really great library for this kind of thing.

over 1 year ago ·

You can alias the hub command in your .dotfiles like so

hub_path=$(which hub)
if [[ -f $hub_path ]]
then
  alias git=$hub_path
fi

that gives you the additional github commands

git pull-request   Open a pull request on GitHub
git fork           Make a fork of a remote repository 
                   on GitHub and add as remote
git create         Create this repository on GitHub and 
                   add GitHub as origin
git browse         Open a GitHub page in the default browser
git compare        Open a compare page on GitHub
over 1 year ago ·

Some Shell Scripts for github
https://github.com/rShetty/Scriptomaniac

over 1 year ago ·

if not using Hub, but instead this method - you still need to remember to 'chmod +x script_name' the script file.

over 1 year ago ·

Great point tchaudhri, thanks!

over 1 year ago ·

Great point tchaudhri, thanks!

over 1 year ago ·

Fine! just modify it if you want to use a token, it's mandatory if you're using a 2 factor authentication:

curl -H "Authorization: token $TOKEN" https://api.github.com/user/repos -d "{\"name\":\"$repo_name\"}"
over 1 year ago ·