49

I have followed these instructions to set up the SSH key for github. But now when I do

> git pull
Username for 'https://github.com': 

in a repository on the local computer I have taken the public SSH key from, I am still asked for a username/password. Did I miss a step?

Alex
  • 41,580
  • 88
  • 260
  • 469
  • 6
    Check `git remote -v` whether you are actually using SSH protocol. – Melebius Sep 21 '17 at 07:11
  • How to see it? I only see something like "origin https://github.com/... (fetch/push)" – Alex Sep 21 '17 at 07:12
  • I have added the output. It seems to be really complicated to setup github so you never ever are asked a username/password when doing a git pull or whatever... – Alex Sep 21 '17 at 07:21
  • 1
    Possible duplicate of [Git keeps prompting me for password](https://stackoverflow.com/questions/7773181/git-keeps-prompting-me-for-password) – phd Sep 22 '17 at 12:08
  • Does this answer your question? [Git push requires username and password](https://stackoverflow.com/questions/6565357/git-push-requires-username-and-password) – Henke Dec 22 '20 at 18:39

3 Answers3

116

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 git@github.com:<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 git@github.com:<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.

Melebius
  • 6,183
  • 4
  • 39
  • 52
  • I have the repository already downloaded. So remove it and start from scratch? – Alex Sep 21 '17 at 07:23
  • No, just run this in the current working tree. It will change the origin’s address and keep everything else. Alternatively, you can setup a new remote by using `git remote add` and then `git pull `. – Melebius Sep 21 '17 at 07:25
  • Very complicated, but it seem to work. I need to write this up somewhere. Or do you know of a webpage where this is nicely explained? – Alex Sep 21 '17 at 07:27
  • It’s described in the GitHub help, link added to my answer. – Melebius Sep 21 '17 at 07:33
  • 1
    Thanks for the help! I wish GitHub had this line on their "How To" page. – Jordan May 05 '21 at 15:09
16

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"]
    url=git@github.com:<usr>/<repo>.git 

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

Melebius
  • 6,183
  • 4
  • 39
  • 52
meesern
  • 2,518
  • 27
  • 29
4

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 git@github.com:vagdevik/dummy.git

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

Thanks!

vagdevi k
  • 1,478
  • 9
  • 25
  • 2
    Oh yeah, my repo doesn't have any previous commits so it didn't hurt me. Otherwise, it is wise to follow the steps you mentioned. – vagdevi k Aug 26 '21 at 17:45