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 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 commented Feb 8, 2018

Thanks!

@ghost

This comment has been minimized.

Copy link

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 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 commented Mar 21, 2018

@Legends I have the same problem

@arpans2112

This comment has been minimized.

Copy link

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 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 commented May 10, 2018

git push -f origin master

@flow2k

This comment has been minimized.

Copy link

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 commented Jun 16, 2018

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

@mphuget

This comment has been minimized.

Copy link

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 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 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 commented Nov 9, 2018

Use git push -f origin master

@brianonn

This comment has been minimized.

Copy link

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 commented Mar 9, 2019

That link helps me alot

@rraj5422

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

@psradebe

This comment has been minimized.

Copy link

psradebe commented Aug 20, 2019

Use git push -f origin master

Thanks

@SAIFULLAH96

This comment has been minimized.

Copy link

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 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)

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.