user2498079
user2498079

Reputation: 3012

Create a GitHub repository from command line

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?

Upvotes: 38

Views: 49280

Answers (8)

VonC
VonC

Reputation: 1328262

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

Upvotes: 0

Boniface B. C.
Boniface B. C.

Reputation: 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

Upvotes: -1

Juan Torres
Juan Torres

Reputation: 77

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 !

Upvotes: 1

MTraveller
MTraveller

Reputation: 31

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

Upvotes: 3

VonC
VonC

Reputation: 1328262

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.

Upvotes: 22

Asclepius
Asclepius

Reputation: 63514

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.

Upvotes: 3

mickiewicz
mickiewicz

Reputation: 289

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

Upvotes: 9

krlmlr
krlmlr

Reputation: 25484

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.

Upvotes: 23

Related Questions