Home > Software engineering >  How to create a github repository from the cli with git using only a token?
How to create a github repository from the cli with git using only a token?

Time:10-29

With github tokens, the easiest way I've found to work with a new repository is.

1. Create the new repo on the github site.
2. git clone https://[user]:[email protected]/[user]/repo-name.git   # Using token to authenticate
3. cd into that new folder and then put files in here and then add / commit / push
git add .  ;  git commit -m "test"  ;  git status  ;   git push -u origin master

This works, but cannot do the process purely from the cli, requiring the step on the site.

However, the "official" way (presented on the github site) is:

echo "# test-xxx" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/[user]/repo-name.git 
git push -u origin master

Obviously, password authentication is no longer available, so this automatically fails. So I updated the remote add origin to be git remove add origin https://[user]:[email protected]/[user]/repo-name.git

But this completely fails with the error:

remote: Repository not found.
fatal: repository 'https://github.com/[user]/repo-name.git/' not found

So the official way is broken (because it assumes that password authentication is going to work, but password authentication was removed in August 2021).

From cli, how do I create a repository under my account (using a token, not a password) and then start pushing changes to that repository?

CodePudding user response:

This works, but cannot do the process purely from the cli, requiring the step on the site.

You don't have to do any step on the site, if you have installed locally the GitHub CLI gh

You can then:


In the discussion, the OP settled on:

Create the repo using gh:

gh auth login --with-token < ~/.token
git init my-project
cd my-project
gh repo create my-project --confirm --public
cd ..; rm -rfi my-project; 
git clone --depth=1 https://[user]:[token]@github.com/[user]/[my-project]
  • Related