如何使用 ssh 密钥而不是密码来克隆/推送到特定的 Github 存储库

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

我已经创建密钥/将密钥关联到我的普通帐户。但我不知道如何强制我的 Mac 上现有的克隆存储库使用 SSH 密钥而不是密码。

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

我在我的 GitHub 帐户中添加了 public 密钥,当我尝试测试它时,它可以正常工作。这就是我所做的:

ssh-keygen -t rsa -C "me@myemailaddress.com"

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

,而不是默认文件夹(因为我在那里已经有另一个密钥)

所以现在我有以下文件:

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...这就是我所做的:

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 密钥。

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

谢谢。

~/.git/config 中的 user 部分只是关于提交作者身份,而不是远程存储库 身份验证

如果您有现有的克隆存储库:

cd /path/to/local/clone
git remote set-url origin git@github.com:<me>/<myRepo>

然后将使用 SSH 而不是 HTTPS。

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

然后添加 SSH 配置文件 (/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>

这将使用您的特定密钥。