dot
dot

Reputation: 15690

how to use ssh key instead of password to clone / push to a specific Gihub repo

I've created / associated the key with my general account. But I don't know how to force an existing cloned repo on my Mac to use the SSH key instead of password.

Here's how I set up my actual key on my GitHub account:

I added the public key in my GitHub account and when I try to test it works. This is what I do:

ssh-keygen -t rsa -C "[email protected]"

When it prompts for the location, instead of the default folder (cuz I already have another key there) I dumped it in Documents/src/github/keys

So now I have the following files:

admins-Mini-3:github admin$ ls -lah keys/
total 16
drwxr-xr-x   4 admin  staff   128B 18 Feb 09:55 .
drwxr-xr-x  10 admin  staff   320B 18 Feb 09:58 ..
-rw-------   1 admin  staff   2.6K 18 Feb 09:55 id_rsa
-rw-r--r--   1 admin  staff   579B 18 Feb 09:55 id_rsa.pub
admins-Mini-3:github admin$ 

And to prove that I can connect to GitHub... this is what I do:

admins-Mini-3:github admin$ ssh -i keys/id_rsa -T [email protected]
Warning: Permanently added the RSA host key for IP address '140.82.113.4' to the list of known hosts.
Enter passphrase for key 'keys/id_rsa': 
Hi me! You've successfully authenticated, but GitHub does not provide shell access.
admins-Mini-3:github admin$ 

But now... I need to change the existing repo on my Mac ... so that when I push to it, it prompts me to use the SSH key.

I edited the .git/config file locally to remove the user info section... hoping it would prompt me either for password or SSH key. But it's not. Can someone point me in the right direction?

Thanks.

Upvotes: 1

Views: 3626

Answers (1)

VonC
VonC

Reputation: 1329262

The user section in ~/.git/config is only about commit authorship, not remote repository authentication.

If you have an existing cloned repository:

cd /path/to/local/clone
git remote set-url origin [email protected]:<me>/<myRepo>

That will then use SSH instead of HTTPS.

But I want to use the key I created in a specific folder.

Then add in a SSH config file (/Users/admin/.ssh/config):

Host gh
  Hostname github.com
  User git
  IdentityFile /path/to/my/key

Then:

cd /path/to/local/clone
git remote set-url origin gh:<me>/<myRepo>

That will use your specific key.

Upvotes: 3

Related Questions