Whenever I’m creating a new project from console I always need to leave it and go to github webpage to create a new repository and come back to console to push the changes.
There some ways to create the repository via CLI.
1. Via cURL
By using the github API and cURL it is really easy to create a new repository:
$ cURL -u 'YOUR_USER_NAME' https://api.github.com/user/repos -d '{"name":"YOUR_REPOSITORY_NAME"}'
From the response is possible to get the git url to add a remote and push the changes:
$ git remote add origin [email protected]:YOUR_USER_NAME/YOUR_REPOSITORY_NAME.git
$ git push origin master
By default a public repository is created, if you wish to create a private one you need to use:
$ cURL -u 'YOUR_USER_NAME' https://api.github.com/user/repos -d '{"name":"YOUR_REPOSITORY_NAME", "description":"This is a private repo" "private":"true", }'
2. Adding a alias to git
It is cumbersome to type the same commands every time, so a git alias make this task pretty straightforward:
$ git config --global alias.gh-create-repo '!sh -c "cURL -u \"YOUR_USER_NAME\" https://api.github.com/user/repos -d \"{\\\"name\\\":\\\"$1\\\"}\"" -'
You can generate a personal access token and add it to the command above, otherwise cURL will prompt you to enter your password.
$ git config --global alias.gh-create-repo '!sh -c "cURL -u \"YOUR_USER_NAME:YOUR_TOKEN\" https://api.github.com/user/repos -d \"{\\\"name\\\":\\\"$1\\\"}\"" -'
From now on you only need to use the alias created above:
$ git init
$ git gh-create-repo my_new_repo
$ git remote add origin [email protected]:YOUR_USER_NAME/YOUR_REPOSITORY_NAME.git
$ git push origin master
It is also possible to enhance the command by making it responsible for add a new remote automatically:
$ git config --global alias.gh-create-repo '!sh -c "cURL -u \"YOUR_USER_NAME:YOUR_TOKEN\" https://api.github.com/user/repos -d \"{\\\"name\\\":\\\"$1\\\"}\" && git remote add origin [email protected]:YOUR_USER_NAME/$1.git" -'
Using this option you just need to create the repository and push the changes:
$ git init
$ git gh-create-repo my_new_repo
$ git push origin master
PS: For a silent output you can use: cURL -s -o /dev/null
3. Using a Ruby Gem
There are some gems out there that not only make it easy to create repository but to perform all the operations provided by Github API.