Skip to content

Instantly share code, notes, and snippets.

@alexpchin
Last active April 17, 2024 02:45
Show Gist options
  • Save alexpchin/dc91e723d4db5018fef8 to your computer and use it in GitHub Desktop.
Save alexpchin/dc91e723d4db5018fef8 to your computer and use it in GitHub Desktop.
Create a new repository on the command line

Setting up a new Git Repo

##Create a new repository on the command line

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:alexpchin/<reponame>.git
git push -u origin master

##Push an existing repository from the command line

git remote add origin git@github.com:alexpchin/<reponame>.git
git push -u origin master
Copy link

ghost commented Dec 17, 2021

If you still need to do the whole create repo on github website.
Just create repo, git clone and then push. So commands are just useless.

@alcorkrishna
Copy link

The above example talks creating repo at user level, how to create remote repo at organizational level. Thanks

@auwsom
Copy link

auwsom commented May 9, 2022

@Amruth-Smaragdine
" curl -u USERNAME:PERSONAL_ACCESS_TOKEN https://api.github.com/user/repos -d '{"name":"myDirName"}' #this will create the repo in github.
This last command will save your Personal Access Token or password in your Bash history. If I find a way to prevent this I will update this comment."

The way to keep the PAC key from bash history is to use one space in front of any command.

If you want to delete the last history line manually use:
history -d $(($HISTCMD - 2)); history -w;
Or find the line number and use:
history -d <line number>; history -w;

@Amondale
Copy link

Amondale commented Aug 1, 2022

since August 2021, using bare HTTPS with a username/password no longer works to create a repo on git. Instead, use a PAT (Personal Access token) created in the web API.

The samples above return:
{ "message": "Requires authentication", "documentation_url": "https://docs.github.com/rest/reference/repos#create-a-repository-for-the-authenticated-user" }

Here's the way that will create a repository using cmdline:

curl -H "Authorization: token {your_personal_access_token}" https://api.github.com/user/repos -d '{"name":"{new_repo_name}"}'

@devxnsh
Copy link

devxnsh commented Oct 19, 2022

@Amondale Thanks for the reply, however I am still getting a Bad Credentials error. I also noticed that this command does not issue username...is there a previous command which you used? Thanks.

@auwsom
Copy link

auwsom commented Oct 19, 2022

I still have some issues when using this command on Linux so I just switched to using the GitHub CLI package (apt install gh) and gh repo create <newrepo> --public. gh auth login has a quick little login utility.

@Amondale
Copy link

hey @devxnsh if you set up your PAT in the web browser correctly, it won't be requiring the username/password, see this article for more info. Now that gh is out of beta, it should be the best approach rather than doing curling, as the Canadians say, eh?/

@jimmy8585
Copy link

if i create new repository in git i want see automatically this step , how to create new repository in command line
image
image

image

@wasibhussain
Copy link

I create a remote repo regarding flutter, and when i push the code to main, it displays that you don't have permission for this repo, access denied,
I think the problem was someone already has created the repo with same name which i tried to put.

@jamiecropley
Copy link

If you go to the repo on your profile it now tells you these instructions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment