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:
Written by Raine
Related protips
9 Responses
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/25333/90347278_2846094488804212_3433259039511805952_o.jpg)
Or you can use Hub which provide some more features.
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/49342/dushanbe-red-500x500.jpg)
Good to know! Thanks hauleth!
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/355/f9961873414a964cb8aa6d81aa2e2293.jpeg)
As already stated, Hub is a really great library for this kind of thing.
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/47788/e6b625008c816ab3d8d742cc3eddbb00.jpeg)
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
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/20311/54588_555132027835563_687647376_o.jpg)
Some Shell Scripts for github
https://github.com/rShetty/Scriptomaniac
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/70932/920336c9691d56f5048491399e5c2ded.png)
if not using Hub, but instead this method - you still need to remember to 'chmod +x script_name' the script file.
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/49342/dushanbe-red-500x500.jpg)
Great point tchaudhri, thanks!
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/49342/dushanbe-red-500x500.jpg)
Great point tchaudhri, thanks!
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\"}"