如何使用ssh密钥而不是密码来克隆/推送到特定的Gihub存储库 [英] how to use ssh key instead of password to clone / push to a specific Gihub repo

查看:47
本文介绍了如何使用ssh密钥而不是密码来克隆/推送到特定的Gihub存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了密钥并将其与我的普通帐户相关联.但是我不知道如何在Mac上强制现有的克隆存储库使用SSH密钥而不是密码.

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.

以下是我在GitHub帐户上设置实际密钥的方法:

我在GitHub帐户中添加了公钥,当我尝试对其进行测试时.这就是我要做的:

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 "me@myemailaddress.com"

当提示输入位置时,我将其转储到Documents/src/github/keys

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$ 

并证明我可以连接到GitHub ...这就是我所做的:

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

但是现在...我需要更改Mac上的现有存储库...,以便在推送到它时,它提示我使用SSH密钥.

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.

我在本地编辑了.git/config文件,以删除用户信息部分...希望它会提示我输入密码或SSH密钥.但事实并非如此.有人可以指出我正确的方向吗?

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?

谢谢.

推荐答案

〜/.git/config 中的 user 部分仅涉及提交作者,而不是远程存储库身份验证.

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

然后它将使用SSH而不是HTTPS.

That will then use SSH instead of HTTPS.

但是我想使用在特定文件夹中创建的密钥.

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

然后添加SSH配置文件(/Users/admin/.ssh/config ):

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

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

然后:

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

这将使用您的特定密钥.

That will use your specific key.

这篇关于如何使用ssh密钥而不是密码来克隆/推送到特定的Gihub存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆