18

I have been trying to push my local repo changes to github from command line. I have been away from git for a while now so I don't remember a few things. For the past hour I have been trying to push the repo without creating a remote repo on Github.com. As far as I remember, git push origin master/ git push is enough to push the local changes and if necessary create a repo on the remote server. However git push wouldn't let me push and automatically create the repo.

So to save time, I created remote repo on github.com and add the remote repo url using

git remote add origin https://mygithubrepoUrl.com

and it worked.

Is it necessary to create remote repo on Github and then add this url from command line to push changes? Can't Git automatically create repo and push changes?

Acumenus
  • 41,481
  • 14
  • 116
  • 107
user2498079
  • 2,324
  • 5
  • 26
  • 45
  • Yes, I believe this is necessary, and creating the repo remotely and then adding `origin` locally is the normal workflow. – Tim Biegeleisen Feb 01 '16 at 07:31
  • 1
    The below post has the answer http://stackoverflow.com/questions/2423777/is-it-possible-to-create-a-remote-repo-on-github-from-the-cli-without-opening-br – Thangakumar D Mar 13 '17 at 16:12
  • Does this answer your question? [Is it possible to create a remote repo on GitHub from the CLI without opening browser?](https://stackoverflow.com/questions/2423777/is-it-possible-to-create-a-remote-repo-on-github-from-the-cli-without-opening-br) – iled Jun 24 '20 at 18:36

4 Answers4

18

You need to create the repo before pushing, but there's hub that automates this for you:

git init newRepo
cd newRepo
hub create

Use the -p switch to hub create to create a private repository. To push the local master branch, issue:

git push -u origin HEAD

The tool can also create pull requests, open the project page, check the CI status, clone existing repos by specifying only username/repo, and a few more things.

The project page suggests aliasing git to hub (because the latter forwards unknown commands to git), but I don't recommend this, even if just to distinguish "bare" Git commands from the hub candy.

krlmlr
  • 22,030
  • 13
  • 107
  • 191
8

Github API should make work.

First create repo using curl and API https://developer.github.com/v3/repos/#create

something like: curl -u 'username' https://api.github.com/user/repos -d '{"name":"repository name"}'

and then you can add remote and push as you have described before:

git remote add origin git@github.com:user/repository_name.git && git push origin master

mickiewicz
  • 259
  • 2
  • 12
  • [Github API create](https://developer.github.com/v3/repos/#create) states that whilst two endpoints exist for creating a new repo (i.e. one for **user accounts** and one for **organization accounts**), they have only enabled the **organization accounts** endpoint. Example `curl --user 'username' https://api.github.com/username/repos --request POST --data '{"name":"newreponame"}' --verbose` returns `404 Not Found`, whereas `curl --user 'username' https://api.github.com/orgs/orgname/repos -d '{"name":"newreponame"}'` returns `201 Created`. Use [hub](https://github.com/github/hub) instead. – Luke Schoen Sep 08 '17 at 22:42
5

cli.github.com is now the successor of hub.

It allows for repository creation from command line, since cli 0.6, and PR 547

Create a new GitHub repository.

Usage:

Create a new GitHub repository.

Use the "ORG/NAME" syntax to create a repository within your organization.

Usage:

gh repo create [<name>] [flags]

Flags:

-d, --description string   Description of repository
    --enable-issues        Enable issues in the new repository (default true)
    --enable-wiki          Enable wiki in the new repository (default true)
-h, --homepage string      Repository home page URL
    --public               Make the new repository public
-t, --team string          The name of the organization team to be granted access

Global Flags:

 --help                  Show help for command
 -R, --repo OWNER/REPO   Select another repository using the OWNER/REPO format
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
1

The answer by mickiewicz using the REST API via curl has numerous deficiencies which are addressed in this answer. Notably, this answer:

  1. authorizes against GitHub using the necessary token authorization, not the obsolete password authentication
  2. makes curl exit with a nonzero code in case of an error (via -f)
  3. parameterizes the repo name
  4. makes a private repo (default is public)

First, obtain a token with access to the repo scope.

REPO_NAME=foo1
GITHUB_TOKEN=0000000000000000000000000000000000000000  # Enter your own.

curl -f -X POST \
  -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" \
  https://api.github.com/user/repos -d "{\"name\": \"${REPO_NAME}\", \"private\": true}"

This answer is relevant only for creating a repository under a user. The request for creating a repository under an organization is slightly different.

If you don't mind installing the GitHub CLI, refer to the answer by VonC instead.

Acumenus
  • 41,481
  • 14
  • 116
  • 107