Git配置、SSH秘钥添加

Mar 17, 2026


MacBookPro 上需要拉取 gitee 上的一个开源仓库,需要配置公钥

生成

ssh-keygen -t rsa -f gitee

配置git

Host gitee
    HostName gitee.com
    User gitee
    IdentityFile ~/.ssh/gitee

配置公钥

在 gitee 上配置公钥(略)

拉取

拉取仓库时报错,提示:

git@gitee.com: Permission denied (publickey).
fatal: Could not read from remote repository.

解决

问了一下通义千问,说:MacBookPro 上的秘钥不会自动添加,需要手动添加,于是执行:

ssh-add gitee

问题解决

其他

通义千问还给出了另外一种方案:在 git 的 config 文件中添加属性,示例如下:

# Gitee
Host gitee.com
  HostName gitee.com
  User git
  IdentityFile ~/.ssh/id_ed25519_gitee
  AddKeysToAgent yes    # ← 关键!自动加载到 agent
  UseKeychain yes       # ← macOS 特有,可保存密码到钥匙串(如有 passphrase)

以后可以试试这种方案