35

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?

3

8 Answers 8

23

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.

3
  • 3
    you need to install hub for your environment from here github.com/github/hub#installation first Commented Oct 14, 2019 at 11:13
  • can it fetch username and password from saved config file?
    – alper
    Commented Feb 20, 2021 at 20:03
  • You need to authenticate once, this stores a token in the user profile that is later used to access GitHub.
    – krlmlr
    Commented Feb 22, 2021 at 4:20
22

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.

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

As noted by itsgus.dev in the comments:

Either --public, --private or --internal flags are required when not running interactively.

5
  • @itsgus.dev Thank you for this feedback. I have included your comment in the answer for more visibility.
    – VonC
    Commented Jul 18, 2022 at 14:31
  • But what if I just want to continue using standard vanilla everywhere git and not download and start using a different GitHub-specific tool just for my git work on GitHub?
    – NeilG
    Commented Nov 19, 2022 at 13:09
  • @NeilG The operations covered by gh are wrappers around GitHub API calls and are not covered by native git commands. Doing so without gh means using curl.
    – VonC
    Commented Nov 20, 2022 at 0:20
  • Thanks @VonC - so I take that as confirmation that GitHub does not support repo creation using vanilla git. You need the GitHub specific command.
    – NeilG
    Commented Nov 22, 2022 at 9:08
  • @NeilG Exactly, that is the idea. Repo creation on GitHub is done through its REST API, hence curl or gh, not git.
    – VonC
    Commented Nov 22, 2022 at 13:17
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 [email protected]:user/repository_name.git && git push origin master

1
  • 1
    Github API 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 instead. Commented Sep 8, 2017 at 22:42
3

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.

0
3

First install GitHub cli

  1. gh repo create - follow instruction
  2. if you choose not to clone locally then continue the below assuming you are inside the working folder else the repo has been cloned into the current folder.
  3. git init
  4. git git remote add origin https://github.com/<user>/<repo>
  5. git branch -M main
  6. now local folder and repo are connected and you are ready to go.
  7. want to delete the repo? first get admin rights if you don't have it.
  8. gh auth refresh -h github.com -s delete_repo
  9. gh repo delete <repo> --confirm
1

For anyone trying to figure out the complete process via the command prompt CLI, here it is:

#1 Install GitHub CLI(gh command) on your local PC if it's not done already

winget install --id GitHub.cli


#2 From console go to your root project path:

cd "C:\Users\jctor\<MY_PROJECT_ROOT>"


#3 Check gh command is recognized on your console.

gh --version


#4 Authenticate first to your remote github dashboard via HTTPS token or 
SSH key(follow prompt)

gh auth login


#5 Execute the "gh command" to create a remote repository in your gitHub 
using the local source code and project's name. 
(source=means root path of project's source code, public= visibility of remote) 

gh repo create --source=. --public --description "Kotlin cours "


#6 Push the local commits to the brand new remote repository on your github 
dashBoard

git push --set-upstream origin main

Done! your remote git repository is up and running !

0

gh repo create, from gh 2.21.0 (Dec. 2022) is more precise when it comes to the new repository owner:

When creating new repos interactively, users are only prompted for a repo name, and it's not immediately clear that names can be prefixed in order to create the repo in an Organization.

Proposed solution

If I enter an un-prefixed name, I'd like to be presented with a select prompt containing my personal account name (the default) and all organizations I am a member of:

owner selection -- https://user-images.githubusercontent.com/245879/200487503-cef84b03-964f-4c31-b502-158f16444219.png


Nov. 2024, with GitHub CLI 2.63.0 and PR 9905 from William Martin, you can now create a push to GitHub a local bare repository:

Given My cwd is a bare git repository
When I run gh repo create --source . --push --private
Then It succeeds in creating and pushing the repo, mirroring all refs

bare git:(feature) ~/workspace/cli/bin/gh repo create williammartin-test-org/bare \
 --source . --push --private --remote bare
✓ Created repository williammartin-test-org/bare on GitHub
-1

this is the script that worked for me

    #!/usr/bin/bash
    
    # Check if the correct number of arguments are provided
    if [ "$#" -ne 1 ]; then
      echo "Usage: $0 <GITHUB_TOKEN>"
      exit 1
    fi
    
    # Assign the first argument to the GITHUB_TOKEN variable
    GITHUB_TOKEN=$1
    
    # Replace these variables with your own values
    REPO_NAME="your_repository_name"
    DESCRIPTION="This is your first repo!"
    HOMEPAGE="https://github.com"
    USERNAME="your_github_username"
    PRIVATE=false
    IS_TEMPLATE=true
    
    # Create a new repository on GitHub
    curl -L \
      -X POST \
      -H "Accept: application/vnd.github+json" \
      -H "Authorization: Bearer $GITHUB_TOKEN" \
      -H "X-GitHub-Api-Version: 2022-11-28" \
      https://api.github.com/user/repos \
      -d "{\"name\":\"$REPO_NAME\",\"description\":\"$DESCRIPTION\",\"homepage\":\"$HOMEPAGE\",\"private\":$PRIVATE,\"is_template\":$IS_TEMPLATE}"
    
    # Initialize the local repository
    git init
    
    # Add files to the repository
    git add .
    
    # Commit the files
    git commit -m "Initial commit"
    
    # Add the remote repository URL
    git remote add origin https://[email protected]/$USERNAME/$REPO_NAME.git
    
    # Push the changes to GitHub
    git push -u origin main
    
    git pull

Save this script as create_and_push_repo.sh. To run it, pass the GitHub token as an argument:

    chmod +x create_and_push_repo.sh
    ./create_and_push_repo.sh your_personal_access_token
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.