Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git: Push a new or existing repo to Github
# 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 https://github.com/c0ldlimit/vimcolors.git
git push -u origin master
# Push an existing repository from the command line
git remote add origin https://github.com/c0ldlimit/vimcolors.git
git push -u origin master
@allanaste

This comment has been minimized.

Copy link

@allanaste allanaste commented Jun 26, 2017

Just one note: if the repository doesn't exist in Github, first you will have to create it: https://help.github.com/articles/creating-a-new-repository/

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Feb 8, 2018

Thanks!

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Feb 25, 2018

can you tell me how to retrieve the commit id from git hub

@Legends

This comment has been minimized.

Copy link

@Legends Legends commented Feb 26, 2018

How to create the repo from cmd?
The above gives me: remote: Repository not found.

@DanielHolth

This comment has been minimized.

Copy link

@DanielHolth DanielHolth commented Mar 21, 2018

@Legends I have the same problem

@arpans2112

This comment has been minimized.

Copy link

@arpans2112 arpans2112 commented Apr 7, 2018

You can't create a repository from command line, Must

if the repository doesn't exist in Github, first you will have to create it: https://help.github.com/articles/creating-a-new-repository/

@shekhar349

This comment has been minimized.

Copy link

@shekhar349 shekhar349 commented May 8, 2018

Shudhanshu Shekhar@SHEKHAR MINGW64 ~/repos/myproj (master)
$ git push -u origin master
fatal: HttpRequestException encountered.
An error occurred while sending the request.
Username for 'https://github.com': shekhar349
To https://github.com/shekhar349/test.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/shekhar349/test.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Please help.

@gitCommitLit

This comment has been minimized.

Copy link

@gitCommitLit gitCommitLit commented May 10, 2018

git push -f origin master

@flow2k

This comment has been minimized.

Copy link

@flow2k flow2k commented May 10, 2018

@Balance-Sun, push -f does not work, at least not over SSH.

@hAbd0u

This comment has been minimized.

Copy link

@hAbd0u hAbd0u commented Jun 16, 2018

@flow2k @Balance-Sun Try this:
git push origin master --force

@mphuget

This comment has been minimized.

Copy link

@mphuget mphuget commented Jun 21, 2018

I confirm, it is required the Git repo already exists and --force is needed instead of -u

@c4arl0s

This comment has been minimized.

Copy link

@c4arl0s c4arl0s commented Sep 16, 2018

Just one note: if the repository doesn't exist in Github, first you will have to create it: https://help.github.com/articles/creating-a-new-repository/

that's what I don´t understand !! why is not possible to create that new repo ?? I was trying to do that push for a while, it drove me crazy�.

@Gflexofficial

This comment has been minimized.

Copy link

@Gflexofficial Gflexofficial commented Oct 16, 2018

$ git init
Reinitialized existing Git repository in C:/Users/G-FLEX/Desktop/Group1/.git/

G-FLEX@DESKTOP-P95SOUR MINGW32 ~/Desktop/Group1 (master)
$ https://github.com/Gflexofficial/first-project.git
bash: https://github.com/Gflexofficial/first-project.git: No such file or directory

G-FLEX@DESKTOP-P95SOUR MINGW32 ~/Desktop/Group1 (master)
$ git add
Nothing specified, nothing added.
Maybe you wanted to say 'git add .'?

G-FLEX@DESKTOP-P95SOUR MINGW32 ~/Desktop/Group1 (master)
$ git add .

G-FLEX@DESKTOP-P95SOUR MINGW32 ~/Desktop/Group1 (master)
$ git commit -m "first project"
[master 8fda73e] first project
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 Group1.exe

G-FLEX@DESKTOP-P95SOUR MINGW32 ~/Desktop/Group1 (master)
$ https://github.com/Gflexofficial/first-project.git
bash: https://github.com/Gflexofficial/first-project.git: No such file or directory

G-FLEX@DESKTOP-P95SOUR MINGW32 ~/Desktop/Group1 (master)
$ git push
fatal: HttpRequestException encountered.
An error occurred while sending the request.
Username for 'https://github.com':
git push
remote: Anonymous access to Project-Create-Web/Group1.git denied.
fatal: Authentication failed for 'https://github.com/Project-Create-Web/Group1.git/'

G-FLEX@DESKTOP-P95SOUR MINGW32 ~/Desktop/Group1 (master)
$ git push
fatal: HttpRequestException encountered.
An error occurred while sending the request.
Username for 'https://github.com': gflexofficial
remote: Permission to Project-Create-Web/Group1.git denied to Gflexofficial.
fatal: unable to access 'https://github.com/Project-Create-Web/Group1.git/': The requested URL returned error: 403

pls what could be the problem

@viragjainVJ

This comment has been minimized.

Copy link

@viragjainVJ viragjainVJ commented Nov 9, 2018

Use git push -f origin master

@brianonn

This comment has been minimized.

Copy link

@brianonn brianonn commented Nov 24, 2018

@viragjainVJ no push -f won't solve this. This is an authentication issue. The user @Gflexofficial is trying to use https to push to a remote, but you can't do that anonymously (without authentication). It needs an ssh key or a username/password

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.

@yomshot

This comment has been minimized.

@yomshot

This comment has been minimized.

Copy link

@yomshot yomshot commented Mar 9, 2019

That link helps me alot

@rraj5422

This comment has been minimized.

Copy link

@rraj5422 rraj5422 commented Mar 15, 2019

For overwriting existing code use this command:
git push -u -f origin master

@smasish

This comment has been minimized.

Copy link

@smasish smasish commented Jun 28, 2019

bash.exe"-3.1$ git push -f origin master
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
bash.exe"-3.1$
How to push my android code to a repository provided by another account?

@vireakpanha-khy

This comment has been minimized.

Copy link

@vireakpanha-khy vireakpanha-khy commented Jul 22, 2019

How to create the repo from cmd?
The above gives me: remote: Repository not found.

You need to go to GitHub and create an empty repository first.

@princeradebe

This comment has been minimized.

Copy link

@princeradebe princeradebe commented Aug 20, 2019

Use git push -f origin master

Thanks

@SAIFULLAH96

This comment has been minimized.

Copy link

@SAIFULLAH96 SAIFULLAH96 commented Aug 23, 2019

Can we push the first commit NOT on master, so that it doesn't get indexed by Google??

@sushantkumar12

This comment has been minimized.

Copy link

@sushantkumar12 sushantkumar12 commented Sep 30, 2019

when I am trying to push my new repo from local to github using git push -f origin master, it fails with below
! [remote rejected] master -> master (pre-receive hook declined)

@HardikGovani

This comment has been minimized.

Copy link

@HardikGovani HardikGovani commented Nov 23, 2019

so there is no such command from cmd, which create & push new repo to remote.

@satyampathak223

This comment has been minimized.

Copy link

@satyampathak223 satyampathak223 commented Jan 11, 2020

can you tell me how to retrieve the commit id from git hub

Use git log command

@nickcox

This comment has been minimized.

Copy link

@nickcox nickcox commented Feb 29, 2020

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.

Another solid option is the hub cli.

@scotopic

This comment has been minimized.

Copy link

@scotopic scotopic commented Jul 20, 2020

git push -u origin master is now
git push -u origin main

@tristanle22

This comment has been minimized.

Copy link

@tristanle22 tristanle22 commented Jul 24, 2020

The problem is probably that the repo was created with a README. Therefore, you can't push the local code since there's a conflict.

@aritram1

This comment has been minimized.

Copy link

@aritram1 aritram1 commented Aug 21, 2020

I confirm, it is required the Git repo already exists and --force is needed instead of -u

Thanks. This worked for me. git push origin master --force

@syedsfayaz

This comment has been minimized.

Copy link

@syedsfayaz syedsfayaz commented Aug 25, 2020

why can't we just clone the new repo "git clone url" and add out code instead of doing git init.

@tristanle22

This comment has been minimized.

Copy link

@tristanle22 tristanle22 commented Aug 26, 2020

@syedsfayaz that works as well. Doing git init means less copy and paste code

@killshot13

This comment has been minimized.

Copy link

@killshot13 killshot13 commented Aug 30, 2020

@syedsfayaz git init is just one of the recommended methods when you create a new repo. As noted previously, regardless you must create the bare bones repo in GitHub before you can do an initial push.

Still, these are the methods of populating a new repo, courtesy of GitHub's canned response upon creation.

QUICK SETUP
git clone https://github.com/your_username/your_new_repository_name.git

CREATE NEW REPO ON THE COMMAND LINE
echo "# temp" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M master
git remote add origin https://github.com/your_username/your_new_repository_name.git
git push -u origin master

PUSH EXISTING REPO FROM THE COMMAND LINE
git remote add origin https://github.com/your_username/your_new_repository_name.git
git branch -M master
git push -u origin master

A final item of note, Mercurial supports direct imports from existing repos already on Github.

@shvam0000

This comment has been minimized.

Copy link

@shvam0000 shvam0000 commented Oct 9, 2020

Github has updated its base branch's name from master to main, and the old commands don't work, could you please help in providing the steps through which I can push a new repo.

@Shreeja-tech

This comment has been minimized.

Copy link

@Shreeja-tech Shreeja-tech commented Oct 11, 2020

why is it showing pathspec "README.md" did not match any files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.