Is it possible to create a remote repo on GitHub from the CLI without opening browser?

155,371

Solution 1

With Github's official new command line interface:

gh repo create

See additional details and options and installation instructions.


For instance, to complete your git workflow:

mkdir project
cd project
git init
touch file
git add file
git commit -m 'Initial commit'
gh repo create
git push -u origin master

Solution 2

CLI commands for github API v3 (replace all CAPS keywords):

curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}'
# Remember replace USER with your username and REPO with your repository/application name!
git remote add origin [email protected]:USER/REPO.git
git push origin master

Solution 3

You can create a GitHub repo via the command line using the GitHub API. Check out the repository API. If you scroll down about a third of the way, you'll see a section entitled "Create" that explains how to create a repo via the API (right above that is a section that explains how to fork a repo with the API, too). Obviously you can't use git to do this, but you can do it via the command line with a tool like curl.

Outside of the API, there's no way to create a repo on GitHub via the command line. As you noted, GitHub doesn't allow shell access, etc., so aside from the GitHub API, the only way to create a repo is through GitHub's web interface.

Solution 4

This can be done with three commands:

curl -u 'nyeates' https://api.github.com/user/repos -d '{"name":"projectname","description":"This project is a test"}'
git remote add origin [email protected]:nyeates/projectname.git
git push origin master

(updated for v3 Github API)


Explanation of these commands...

Create github repo

    curl -u 'nyeates' https://api.github.com/user/repos -d '{"name":"projectname","description":"This project is a test"}'
  • curl is a unix command (above works on mac too) that retrieves and interacts with URLs. It is commonly already installed.
  • "-u" is a curl parameter that specifies the user name and password to use for server authentication.
    • If you just give the user name (as shown in example above) curl will prompt for a password.
    • If you do not want to have to type in the password, see githubs api documentation on Authentication
  • "-d" is a curl parameter that allows you to send POST data with the request
  • "name" is the only POST data required; I like to also include "description"
  • I found that it was good to quote all POST data with single quotes ' '

Define where to push to

git remote add origin [email protected]:nyeates/projectname.git
  • add definition for location and existance of connected (remote) repo on github
  • "origin" is a default name used by git for where the source came from
    • technically didnt come from github, but now the github repo will be the source of record
  • "[email protected]:nyeates" is a ssh connection that assumes you have already setup a trusted ssh keypair with github.

Push local repo to github

git push origin master
  • push to the origin remote (github) from the master local branch

Solution 5

If you install defunkt's excellent Hub tool, then this becomes as easy as

hub create

In the words of the author, "hub is a command-line wrapper for git that makes you better at GitHub."

Share:
155,371
anddoutoi
Author by

anddoutoi

Updated on July 18, 2022

Comments

  • anddoutoi
    anddoutoi almost 2 years

    I created a new local Git repository:

    ~$ mkdir projectname
    ~$ cd projectname
    ~$ git init
    ~$ touch file1
    ~$ git add file1
    ~$ git commit -m 'first commit'
    

    Is there any git command to create a new remote repo and push my commit to GitHub from here? I know it's no big deal to just fire up a browser and head over to Create a New Repository, but if there is a way to achieve this from the CLI I would be happy.

    I read a vast amount of articles but none that I found mention how to create a remote repo from the CLI using git commands. Tim Lucas's nice article Setting up a new remote git repository is the closest I found, but GitHub does not provide shell access.