14

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
  • 34,021
  • 64
  • 178
  • 371
  • 2
    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

2 Answers2

39

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
  • 4,692
  • 3
  • 32
  • 43
  • 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
  • Thanks for the help! I wish GitHub had this line on their "How To" page. – Jordan May 05 '21 at 15:09
3

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

Change to

[remote "origin"]j
    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'.

meesern
  • 1,908
  • 21
  • 28