Why is github asking me username/password although I setup SSH authentication?

You need to tell Git to use SSH protocol instead of HTTPS. On the repository page on GitHub, select Clone or Download and Use SSH. You will get a URL for the SSH protocol in the form [email protected]:<user>/<repo>.git.

Then run the following command in your working tree to tell Git to use this URL instead of the current one:

git remote set-url origin [email protected]:<user>/<repo>.git

This is also explained in the GitHub Help.

The method above won’t cause the repository to be cloned again, it just changes the communication protocol used for future synchronization between your local repo and GitHub.

Alternatively, you could set up a new remote using git remote add <new-remote-name> <url> and then git pull <new-remote-name> but Git would keep track of both protocols as separate remotes, so I do not recommend this.


This can also be done by editing the git config file for your project. With your favourite editor open .git/config and find the existing URL:

[remote "origin"]
    url=https://github.com/<usr>/<repo>.git 

Change to:

[remote "origin"]
    [email protected]:<usr>/<repo>.git 

Personally, I find this a bit easier to remember at the risk of being a little more 'internal'.


In my case, I have created my own repo on GitHub, and wanted to push files to that repo from my local machine. When I wanted to push the code, I was asked to type the username and password though I have configured my GitHub account with a public SSH key.

My mistake was that I have added the remote with https end-point instead of the ssh one, as follows:

git remote add origin https://github.com/vagdevik/dummy.git

I fixed the issue by:

(The following points are important because, if we directly jump to step 2, it throws an error saying that the remote already exixts)

  1. Removing the .git and initialize the tracking again:

rm -rf .git

Initialize the git tracking again, added files to track, and committed :

git init
git add .
git commit -m "first commit"

Note: I didn't do any previous commits to this repo, so removing .git doesn't hurt. If you don't want to lose the changes you made to your repo, please follow this.

  1. Now, add the SSH remote as follows, instead of the one with https endpoint(IMPORTANT)

git remote add origin [email protected]:vagdevik/dummy.git

This time, I was not asked to enter username/password.

Thanks!

Tags:

Git

Ssh

Github