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

25

398

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.

Conjunction answered 11/3, 2010 at 9:25 Comment(0)
C
33

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
Calyptra answered 27/4, 2020 at 13:37 Comment(0)
O
349

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
Overlord answered 25/4, 2012 at 23:38 Comment(14)
The slight problem with first command is that you are leaving your GitHub password in your ~/.bash_history. I would suggest replace -u 'USER:PASS' with -u 'USER', then curl will ask you for password interactively.Addi
To make the repo private from the start, use: curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO", "private":"true"}'Venu
I wrote a bash script to save us all some typing. Takes user input and has sensible defaults: gist.github.com/robwierzbowski/5430952Ainu
Here's a Git alias for this.Standstill
Here's how to add it as a git alias: git config --global alias.gh-create '!sh -c "curl -u \"USERNAME\" https://api.github.com/user/repos -d \"{\\\"name\\\":\\\"$1\\\"}\"" -'Scarbrough
Actually @MattTagg's comment below is now my preferred solution - Hub is a brilliant project for managing all sorts of GitHub actions on the command-line.Scarbrough
If using an org account, need to change the curl command to use .../orgs/ORGNAME/repos instead of .../user/repos, like curl -u 'USER' https://api.github.com/orgs/ORGNAME/repos -d '{"name":"REPO"}'Dani
Don't forget that you can generate an access token and use it this way: curl https://api.github.com/user/repos?access_token=myAccessToken -d '{"name":"REPO"}'. :-)Nierman
oddly git remote add origin [email protected]:USER/REPO.git (SSH URL) did not work for me, but the HTTPS URL form does: git remote add origin https://github.com/USER/REPO.gitDenominationalism
powershell script (I have curl as an alias for cygwin's curl and $ghUsername predefined in my profile) function gh-create { $repoName = $args[0]; $data = @{ name=$repoName }; $data | ConvertTo-Json -Compress | curl -H "Content-Type: application/json" -u $ghUsername https://api.github.com/user/repos -d "@-" } Yelena
@IonicăBizău I'm getting { "message": "Not Found", "documentation_url": "https://developer.github.com/v3" } from your command.Clad
Better yet, store your credentials in a local file which you keep secure like your ssh key. Then write a script that reads the credentials and issues the curl command.Laager
For the record, this answer is outdated: password authentication is deprecated and will be removed soon. I have posted the a new procedure here: https://mcmap.net/q/86557/-is-it-possible-to-create-a-remote-repo-on-github-from-the-cli-without-opening-browserTrellas
If you are finding that you are having Requires authentication issues with the accepted answer - check out the official git docs: docs.github.com/en/rest/guides/… tldr : bash curl -i -H "Authorization: token ghp_16C7e42F292c6912E7710c838347Ae178B4a" -d '{ "name": "blog", "auto_init": true, "private": true, "gitignore_template": "nanoc" }' https://api.github.com/user/repos is the full way of doing thisCartwell
O
227

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.

Oath answered 11/3, 2010 at 14:20 Comment(11)
Thanks a bunch mipadi! Didn´t know about the GitHub API. For everyone else with the same problem, this is what i basicly did: curl -F 'login=username' -F 'token=API Token' https://github.com/api/v2/yaml/repos/create -F name=reponame. Your API Token can be found on the GitHub site, click Account Settings, look for Administrative Information and API Token (32 character long string).Conjunction
It seems this is out of date, at least I do not find the API Token there.Twibill
Yes, version 2 of the API is marked as obsolete and they want us to use version 3. I havn´t yet grokked v3 but when I get the time I will try to wrap my head around it.Conjunction
API Version 3 Syntax given below via @Overlord https://mcmap.net/q/86557/-is-it-possible-to-create-a-remote-repo-on-github-from-the-cli-without-opening-browserMongol
Guess sometimes people have to agree to disagree. If you have Github set up with the proper security configuration and certificates, using a transport layer security model, you should not have to use the API to create a new repo. Mark my words should. If it is like that it is not a good implementation. I've switched from Github to Mercurial. If you get to choose between MacGyver and James Bond, choose the latter!Gospel
@cseder: Git doesn't require this to create a repo, but setting up one on GitHub does. I don't think Mercurial lets you create a repo on a remote server by pushing to a nonexistent repo, either.Oath
No, it works in a slightly different manner. My point isn't to push code to a non-existing repo. What would be the point of trying to do so? I'm talking about initializing and committing from the command-line as the question was all about. Quote: Is there any git command to create a new remote repo and push my commit to GitHub from here? Roger Out.Gospel
@cseder: The question is asking if it's possible to create a remote repo on GitHub via the GitHub API, not how to create a new repo and push to an existing one on GitHub.Oath
@Oath Ok. Misunderstood then. That happens to me sometimes. I'm human I think...Gospel
@Oath Sorry for sounding like a newbie, but what do you mean by Obviously you can't use git to do this? It isn't so obvious to me, if you could help explain...Dorthydortmund
@JohnStrood: It is a well known fact, that you can't create a remote repo using git init --remote or something like that. You will have to go to the remote host, create the repo there using git init and then you can push to that (assuming you have all necessary access rights). Obviously, you can't do that without shell access (that github doesn't provide) so you have to use other means to create the github repo - here: Use the API that github provides, which obviously is not part of the git protocol. That's what @Oath meant, when he said, Obviously you can't use git to do this.Cathcart
T
78

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
Trunks answered 27/9, 2011 at 3:14 Comment(0)
S
54

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."

Sodality answered 13/11, 2012 at 18:20 Comment(2)
I LOVE hub! Also useful, is the hub - or as hub is usually aliases to git... git fork, which creates a fork of the repo for the pwd of a cloned repo that you are in... Yay.Tutorial
this tool is awesome! It takes care of storing your authentication token for you so you don't have to type your password over and over. Also checkout the ZSH plugin for github.Causeuse
C
33

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
Calyptra answered 27/4, 2020 at 13:37 Comment(0)
P
21

Simple steps (using git + hub => GitHub):

  1. Install Hub (GitHub).

    • OS X: brew install hub
    • having Go: go get github.com/github/hub
    • otherwise (having Go as well):

      git clone https://github.com/github/hub.git && cd hub && ./script/build
      
  2. Go to your repo or create empty one: mkdir foo && cd foo && git init.

  3. Run: hub create, it'll ask you about GitHub credentials for the first time.

    Usage: hub create [-p] [-d DESCRIPTION] [-h HOMEPAGE] [NAME]

    Example: hub create -d Description -h example.com org_name/foo_repo

    Hub will prompt for GitHub username & password the first time it needs to access the API and exchange it for an OAuth token, which it saves in ~/.config/hub.

    To explicitly name the new repository, pass in NAME, optionally in ORGANIZATION/NAME form to create under an organization you're a member of.

    With -p, create a private repository, and with -d and -h set the repository's description and homepage URL, respectively.

    To avoid being prompted, use GITHUB_USER and GITHUB_PASSWORD environment variables.

  4. Then commit and push as usual or check hub commit/hub push.

For more help, run: hub help.

See also: Importing a Git repository using the command line at GitHub.

Peruzzi answered 3/8, 2015 at 19:13 Comment(3)
How can I set GITHUB_USER and GITHUB_PASSWORD environment variables ?Tooth
Probably you can export them, see: GH #245.Peruzzi
Worked great for me, note "hub" is also available on MacPorts.Bondswoman
J
11

There is an official github gem which, I think, does this. I'll try to add more information as I learn, but I'm only just now discovering this gem, so I don't know much yet.

UPDATE: After setting my API key, I am able to create a new repo on github via the create command, however I am not able to use the create-from-local command, which is supposed to take the current local repo and make a corresponding remote out on github.

$ gh create-from-local
=> error creating repository

If anyone has some insight on this, I'd love to know what I'm doing wrong. There's already an issue filed.

UPDATE: I did eventually get this to work. I'm not exactly sure how to re-produce the issue, but I just started from scratch (deleted the .git folder)

git init
git add .emacs
git commit -a -m "adding emacs"

Now this line will create the remote repo and even push to it, but unfortunately I don't think I can specify the name of the repo I'd like. I wanted it to be called "dotfiles" out on github, but the gh gem just used the name of the current folder, which was "jason" since I was in my home folder. (I added a ticket asking for the desired behavior)

gh create-from-local

This command, on the other hand, does accept an argument to specify the name of the remote repo, but it's intended for starting a new project from scratch, i.e. after you call this command, you get a new remote repo that's tracking a local repo in a newly-created subfolder relative to your current position, both with the name specified as the argument.

gh create dotfiles
Jetty answered 8/4, 2011 at 4:36 Comment(1)
This project hasn't had any work on it for a couple of years, didn't work for me, and, as implied here, is dead. It has apparently been replaced by the hub tool, as suggested in this answer.Pot
C
10

To Quickly Create the Remote Repository by Using a Bash Shell

It is cumbersome to type the complete code every time a repository is to be created

curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}'
git remote add origin [email protected]:USER/REPO.git
git push origin master

An easier approach is:

  1. create a shell script in a directory i.e. /home/USER_NAME/Desktop/my_scripts named githubscript.sh
  2. Modify and save the following code to the githubscript.sh file
#!bin/bash
curl -u 'YOUR_GITHUB_USER_NAME' https://api.github.com/user/repos -d "{\"name\":\"$1\"}";
git init;
git remote add origin [email protected]:YOUR_GITHUB_USER_NAME/$1.git;

N.B. Here $1 is the repository name that is passed as an argument when invoking the script Change YOUR_GITHUB_USER_NAME before saving the script.

  1. Set required permissions to the script file
chmod 755 githubscript.sh
  1. Include the scripts directory in the environment configuration file.
nano ~/.profile;
export PATH="$PATH:$HOME/Desktop/my_scripts"
  1. Also set an alias to run the githubscript.sh file.
nano ~/.bashrc;
alias githubrepo="bash githubscript.sh"
  1. Now reload the .bashrc and .profile files in the terminal.
source ~/.bashrc ~/.profile;
  1. Now to create a new repository i.e. demo:
githubrepo demo;
Cuneiform answered 5/1, 2015 at 5:53 Comment(1)
In your code, I changed this part: git remote add origin [email protected]:YOUR_GITHUB_USER_NAME/$1.git; to git remote add origin https://github.com/YOUR_GITHUB_USER_NAME/$1.git; For users, who don't use SSH key.Extine
T
7

Both the accepted answer and the most-voted answer so far are now outdated. Password authentication is deprecated and it will be removed on November 13, 2020 at 16:00 UTC.

The way to use GitHub API now is via personal access tokens.

You need to (replace ALL CAPS keywords):

  1. Create a personal access token via the website. Yes, you have to use the browser, but it is only once for all future accesses. Store the token securely.
  2. Create the repo via
curl -H 'Authorization: token MY_ACCESS_TOKEN' https://api.github.com/user/repos  -d '{"name":"REPO"}'

or, to make it private from the start:

curl -H 'Authorization: token MY_ACCESS_TOKEN' https://api.github.com/user/repos -d '{"name":"REPO", "private":"true"}'
  1. Add the new origin and push to it:
git remote add origin [email protected]:USER/REPO.git
git push origin master

This has the disadvantage that you have to type the token each time, and that it appears in your bash history.

To avoid this, you can

  1. Store the header in a file (let's call it, say, HEADER_FILE)
Authorization: token MY_ACCESS_TOKEN
  1. Have curl read from the file
curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO"}' # public repo
curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO", "private":"true"}' # private repo
  1. To make things more secure, you could set access permissions to 400 and the user to root
chmod 400 HEADER_FILE
sudo chown root:root HEADER_FILE
  1. Now sudo will be needed to access the header file
sudo curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO"}' # public repo
sudo curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO", "private":"true"}' # private repo
Trellas answered 19/10, 2020 at 7:47 Comment(0)
P
6

Based on the other answer by @Mechanical Snail, except without the use of python, which I found to be wildly overkill. Add this to your ~/.gitconfig:

[github]
    user = "your-name-here"
[alias]
    hub-new-repo = "!REPO=$(basename $PWD) GHUSER=$(git config --get github.user); curl -u $GHUSER https://api.github.com/user/repos -d {\\\"name\\\":\\\"$REPO\\\"} --fail; git remote add origin [email protected]:$GHUSER/$REPO.git; git push origin master"
Philharmonic answered 8/3, 2015 at 7:19 Comment(1)
I love this alias. Thanks again, @Robru. P.S. if this doesn't work, or stops working after a fresh OS install.. make sure you have curl installed!Blintz
I
5

Update 20200714

Github has a new official CLI.

...gh is a new project that helps us explore what an official GitHub CLI tool can look like with a fundamentally different design. While both tools bring GitHub to the terminal, hub behaves as a proxy to git, and gh is a standalone tool.

The core difference from hub is that this does not wrap over existing git commands. So, you can not alias git to gh like you can with hub.

We didn't add it to hub because we decided to design GitHub CLI differently than being a git wrapper, i.e. as its own command. It turns out, maintaining an executable that's a proxy to git is hard to maintain, and also we didn't want to be limited by having to always keep git compatibility. We didn't want to build GitHub CLI on a paradigm that is brittle from the start.

-- mislav (Maintainer of hub)

Original answer

What you need is hub. Hub is a command-line wrapper for git. It has been made to integrate with native git using alias. It tries to provide github actions into git including creating new repository.

→  create a repo for a new project
$ git init
$ git add . && git commit -m "It begins."
$ git create -d "My new thing"
→  (creates a new project on GitHub with the name of current directory)
$ git push origin master
Irreformable answered 24/3, 2014 at 7:50 Comment(0)
S
5

Nope, you have to open a browser atleast once to create your username on GitHub, once created, you can leverage GitHub API to create repositories from command line, following below command:

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

For example:

curl -u 'arpitaggarwal' https://api.github.com/user/repos -d '{"name":"command-line-repo"}'
Semifinal answered 3/2, 2017 at 17:9 Comment(1)
Then git remote add origin https://github.com/github-username/repo-name.git to link your local project to github. For the example, the command would look like this: git remote add origin https://github.com/arpitaggarwal/command-line-repo.gitCatie
C
5

Finally, it happened GitHub has officially announced their new CLI for all the core features.

check here: https://cli.github.com/

To install via HomeBrew: brew install gh for other Ways : https://github.com/cli/cli#installation

then

gh repo create

Other available features.

$ gh --help

Work seamlessly with GitHub from the command line.

USAGE
  gh <command> <subcommand> [flags]

CORE COMMANDS
  gist:       Create gists
  issue:      Manage issues
  pr:         Manage pull requests
  release:    Manage GitHub releases
  repo:       Create, clone, fork, and view repositories

ADDITIONAL COMMANDS
  alias:      Create command shortcuts
  api:        Make an authenticated GitHub API request
  auth:       Login, logout, and refresh your authentication
  completion: Generate shell completion scripts
  config:     Manage configuration for gh
  help:       Help about any command

FLAGS
  --help      Show help for command
  --version   Show gh version

EXAMPLES
  $ gh issue create
  $ gh repo clone cli/cli
  $ gh pr checkout 321

ENVIRONMENT VARIABLES
  See 'gh help environment' for the list of supported environment variables.

LEARN MORE
  Use 'gh <command> <subcommand> --help' for more information about a command.
  Read the manual at https://cli.github.com/manual

FEEDBACK
  Open an issue using 'gh issue create -R cli/cli'

So now you can create repo from your terminal.

Carnegie answered 21/9, 2020 at 3:46 Comment(0)
T
4

For users with two-factor authentication, you can use bennedich's solution, but you just need to add the X-Github-OTP header for the first command. Replace CODE with the code that you get from the two-factor authentication provider. Replace USER and REPO with the username and name of the repository, as you would in his solution.

curl -u 'USER' -H "X-GitHub-OTP: CODE" -d '{"name":"REPO"}' https://api.github.com/user/repos
git remote add origin [email protected]:USER/REPO.git
git push origin master
Triquetrous answered 15/12, 2013 at 19:27 Comment(0)
C
3

I wrote a nifty script for this called Gitter using the REST APIs for GitHub and BitBucket:

https://github.com/dderiso/gitter

BitBucket:

gitter -c -r b -l javascript -n node_app

GitHub:

gitter -c -r g -l javascript -n node_app
  • -c = create new repo
  • -r = repo provider (g = GitHub, b = BitBucket)
  • -n = name the repo
  • -l = (optional) set the language of the app in the repo
Canna answered 2/2, 2013 at 20:19 Comment(0)
F
3

For Rubyists:

gem install githubrepo
githubrepo create *reponame*

enter username and pw as prompted

git remote add origin *ctrl v*
git push origin master

Source: Elikem Adadevoh

Fawcett answered 24/4, 2014 at 0:26 Comment(0)
E
3

For all the Python 2.7.* users. There is a Python wrapper around the Github API that is currently on Version 3, called GitPython. Simply install using easy_install PyGithub or pip install PyGithub.

from github import Github
g = Github(your-email-addr, your-passwd)
repo = g.get_user().user.create_repo("your-new-repos-name")

# Make use of Repository object (repo)

The Repository object docs are here.

Easternmost answered 19/12, 2016 at 9:43 Comment(0)
E
2

For directions on creating a token, go here This is the command you will type (as of the date of this answer. (replace all CAPS keywords):

curl -u 'YOUR_USERNAME' -d '{"scopes":["repo"],"note":"YOUR_NOTE"}' https://api.github.com/authorizations

Once you enter your password you will see the following which contains your token.

{
  "app": {
    "name": "YOUR_NOTE (API)",
    "url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api"
  },
  "note_url": null,
  "note": "YOUR_NOTE",
  "scopes": [
    "repo"
  ],
  "created_at": "2012-10-04T14:17:20Z",
  "token": "xxxxx",
  "updated_at": "2012-10-04T14:17:20Z",
  "id": xxxxx,
  "url": "https://api.github.com/authorizations/697577"
}

You can revoke your token anytime by going here

Eason answered 4/10, 2012 at 14:33 Comment(0)
S
2

I've created a Git alias to do this, based on Bennedich's answer. Add the following to your ~/.gitconfig:

[github]
    user = "your_github_username"
[alias]
    ; Creates a new Github repo under the account specified by github.user.
    ; The remote repo name is taken from the local repo's directory name.
    ; Note: Referring to the current directory works because Git executes "!" shell commands in the repo root directory.
    hub-new-repo = "!python3 -c 'from subprocess import *; import os; from os.path import *; user = check_output([\"git\", \"config\", \"--get\", \"github.user\"]).decode(\"utf8\").strip(); repo = splitext(basename(os.getcwd()))[0]; check_call([\"curl\", \"-u\", user, \"https://api.github.com/user/repos\", \"-d\", \"{{\\\"name\\\": \\\"{0}\\\"}}\".format(repo), \"--fail\"]); check_call([\"git\", \"remote\", \"add\", \"origin\", \"[email protected]:{0}/{1}.git\".format(user, repo)]); check_call([\"git\", \"push\", \"origin\", \"master\"])'"

To use it, run

$ git hub-new-repo

from anywhere inside the local repository, and enter your Github password when prompted.

Standstill answered 27/5, 2013 at 4:3 Comment(3)
This did not work for me. It returns 'No such file or directory'Millhon
This did not work for me either. It returns curl: (22) The requested URL returned error: 401 Traceback (most recent call last): File "<string>", line 1, in <module> File "/usr/lib64/python3.2/subprocess.py", line 488, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['curl', '-u', 'myusername', 'https://api.github.com/user/repos', '-d', '{"name": "reponame"}', '--fail']' returned non-zero exit status 22Hy
The use of python is a bit much and adds a lot of noise in the form of extra backslashes and other puncutation. I made a version with just bash: https://mcmap.net/q/86557/-is-it-possible-to-create-a-remote-repo-on-github-from-the-cli-without-opening-browserPhilharmonic
O
2

Found this solution which I liked: https://medium.com/@jakehasler/how-to-create-a-remote-git-repo-from-the-command-line-2d6857f49564

You first need to create a Github Personal Access Token

Open up your ~/.bash_profile or ~/.bashrc in your favorite text editor. Add the following line near the top of your file, where the rest of the export ‘ed variables are:

export GITHUB_API_TOKEN=<your-token-here>

Somewhere below, by your other bash functions, you can paste something similar to the following:

function new-git() {
    curl -X POST https://api.github.com/user/repos -u <your-username>:$GITHUB_API_TOKEN -d '{"name":"'$1'"}'
}

Now, whenever you’re creating a new project, you can run the command $ new-git awesome-repo to create a new public remote repository on your Github user account.

Obbligato answered 22/9, 2017 at 17:3 Comment(0)
T
2

Disclamier: I'm the author of the open source project

This functionality is supported by: https://github.com/chrissound/Human-Friendly-Commands essentially it is this script:

#!/usr/bin/env bash

# Create a repo named by the current directory
# Accepts 1 STRING parameter for the repo description
# Depends on bin: jq
# Depends on env: GITHUB_USER, GITHUB_API_TOKEN
github_createRepo() {
  projName="$(basename "$PWD")"
  json=$(jq -n \
    --arg name "$projName" \
    --arg description "$1" \
    '{"name":$name, "description":$description}')

  curl -u "$GITHUB_USER":"$GITHUB_API_TOKEN" https://api.github.com/user/repos -d "$json"
  git init
  git remote add origin [email protected]:"$GITHUB_USER"/"$projName".git
  git push origin master
};
Trochaic answered 7/1, 2018 at 16:31 Comment(1)
This is indeed the new way for using a GitHub Personal Access Token. (The old way of adding it via ?access_token=${ACCESSTOKEN} no longer works.Charitycharivari
A
1

The answer is:

> # Install gh
> brew install gh
> gh auth login # will take you to the browser but there are other options for non-interactive
> gh repo create repoName --private --source=. --remote=origin
> git remote -v # will show the local is connected
> git push -u origin main # will push to that connection

For those who want a bash script here is my MACOS zsh script add this at the end of your project create process to put your project into github

It assumes all installs are completed or included in your script and you are auth'd:

# Prompt the user for confirmation to push to GitHub
read -p "Do you want to create repo and push to GitHub? (y/N): " choice
choice=${choice:-N} # Default to 'N' if Enter is pressed

if [[ "$choice" =~ ^[Yy]$ ]]; then

  #adds custom ssh keys as per your configuration
  ssh-add --apple-load-keychain

  read -p "Enter the GitHub repository name: " repo_name
  read -p "Enter the GitHub organization/user (or leave it empty for your user): " repo_user

  if [ -z "$repo_user" ]; then
    repo_user="$(git config user.username)" # Use your GitHub username from git config
  fi

  repo_url="[email protected]:$repo_user/$repo_name.git"

  # Add all changes to the repo
  git add -A
  git commit -m "First commit"

  #create remote Repo
  gh repo create $repo_name --private --source=. --remote=origin
  # Process the project out to a new main branch
  git push -u origin main
  # Create a new dev branch
  git checkout -b dev
  git push -u origin dev

  echo "GitHub push completed."
else
  echo "No action taken."
fi

If anyone wants a PowerShell script just ask and I will add here:... it wont be much different

Assiniboine answered 22/10, 2023 at 7:33 Comment(0)
A
0

For rep reasons, I can't add this as a comment (where it would better go with bennedich's answer), but for Windows command line, here is the correct syntax:

curl -u YOUR_USERNAME https://api.github.com/user/repos -d "{\"name\":\"YOUR_REPO_NAME\"}"

It's the same basic form, but you have to use double quotes (") instead of single, and escape the double quotes sent in the POST parameters (after the -d flag) with backslashes. I also removed the single quotes around my username, but if your username had a space (possible?) it would probably need double quotes.

Aikoail answered 23/9, 2014 at 4:59 Comment(2)
Good to know for Windows users. No, usernames cannot contain spaces (The sign-up form at github.com states: "Username may only contain alphanumeric characters or single hyphens, and cannot begin or end with a hyphen"). Thus, no double-quoting of the username needed.Meaganmeager
Github power shell does not accept -u with curl, on Windows :(Palpitate
P
-1

here is my initial git commands (possibly, this action takes place in C:/Documents and Settings/your_username/):

mkdir ~/Hello-World
# Creates a directory for your project called "Hello-World" in your user directory
cd ~/Hello-World
# Changes the current working directory to your newly created directory
touch blabla.html
# create a file, named blabla.html
git init
# Sets up the necessary Git files
git add blabla.html
# Stages your blabla.html file, adding it to the list of files to be committed
git commit -m 'first committttt'
# Commits your files, adding the message 
git remote add origin https://github.com/username/Hello-World.git
# Creates a remote named "origin" pointing at your GitHub repository
git push -u origin master
# Sends your commits in the "master" branch to GitHub
Pavilion answered 28/4, 2014 at 14:38 Comment(1)
Hello-World repo should be available at GitHub in this case, does not address the problem in the question.Palpitate
P
-6

create a new repository on the command line

echo "# <RepositoryName>" >> README.md

git init

git add README.md

git commit -m "first commit"

git remote add origin https://github.com/**<gituserID>/<RepositoryName>**.git

git push -u origin master

push an existing repository from the command line

git remote add origin https://github.com/**<gituserID>/<RepositoryName>**.git

git push -u origin master
Patriciapatrician answered 8/7, 2016 at 11:17 Comment(1)
This does not work; it doesn't create the remote repository.Undergo

© 2022 - 2024 — McMap. All rights reserved.