Skip to content

Instantly share code, notes, and snippets.

@brianonn
Created November 24, 2018 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianonn/c2be7676d499216f17bdba18acc8fa60 to your computer and use it in GitHub Desktop.
Save brianonn/c2be7676d499216f17bdba18acc8fa60 to your computer and use it in GitHub Desktop.
new git repo
touch README.md
git init
git add README.md
git commit -m "first commit"

## push to remote
# the remote repository must already exist 
git remote add origin https://github.com/brianonn/newrepo.git
git push -u origin master

The remote URL can be either HTTPS or SSH based.

To use an HTTPS remote URL the remote should be specified as : https://<your_username>@github.com/<repo_owner>/repo.git

If you are not the owner of the repo then you will need the owner to give you write access first. When you push to an HTTPS remote, git will ask you for your password on the remote. This can be your GitHub password or a Personal Access Token (under Settings->Developer->Personal Access Tokens) If you have 2FA enabled on your account then the password you use must be a Personal Access Token. It will not accept your GitHub account password anymore.

To use SSH to contact the origin server, the remote should be specified like this: git@github.com:<repo_owner>/repo.git The repo owner must have given you write access first, AND you must provide the repo owner with your ssh public key. If you are the repo owner then you must put your ssh public key under Settings->SSH and GPG Keys

And finally, to answer the question most asked here, how to create the repo from the command line, the answer is to use curl and the GitHub API to create the repo before you push to it:

curl -u <your_username> \ 
       -H "Content-Type: application/json" \ 
       -d '{"name":"<repo_name>"}' https://api.github.com/user/repos

(The above is all one line, with line-breaks. Windows users will want to do it all in one line, use double quotes and escape escape double quotes with a backslash " )

Replace <repo_name> with the name of the repo you want to create. Curl will ask you for your password. You can use your GitHub account password or a Personal Access Token (see above). If you use your GitHub account password and your account is also protected by 2FA you must add the header X-GitHub-OTP: 999999 with the current 6 digit code from your Google Authenticator app replacing the 999999. If you use a Personal Access Token for your password with curl then you don't need to supply the X-GitHub-OTP header.

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