git使用ssh

495 阅读1分钟

检查现有 SSH 密钥

  1. 打开 Git Bash。

  2. 输入 ls -al ~/.ssh 以查看是否存在现有 SSH 密钥:

    $ ls -al ~/.ssh
    # Lists the files in your .ssh directory, if they exist
    
  3. 检查目录列表以查看是否已经有 SSH 公钥。 默认情况下,公钥的文件名是以下之一:

    • id_rsa.pub
    • id_ecdsa.pub
    • id_ed25519.pub

生成新 SSH 密钥并添加到 ssh-agent

生成新 SSH 密钥

  1. 打开 Git Bash。

  2. 粘贴下面的文本(替换为您的 GitHub 电子邮件地址)。

    $ ssh-keygen -t ed25519 -C "your_email@example.com"
    

    **注:**如果您使用的是不支持 Ed25519 算法的旧系统,请使用以下命令:

    $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    
    # 或者简单点
    ssh-keygen -t rsa -C "xxxxx@xxxxx.com"  
    # 这里的 xxxxx@xxxxx.com 只是生成的 sshkey 的名称,并不约束或要求具体命名为某个邮箱
    # 按照提示完成三次回车,即可生成 ssh key。通过查看 ~/.ssh/id_rsa.pub 文件内容,获取到你的 public key
    # C:\Users\sheng\.ssh
    

    这将创建以所提供的电子邮件地址为标签的新 SSH 密钥。

    > Generating public/private ed25519 key pair.
    
  3. 提示您“Enter a file in which to save the key(输入要保存密钥的文件)”时,按 Enter 键。 这将接受默认文件位置。

    > Enter a file in which to save the key (/c/Users/you/.ssh/id_ed25519):[Press enter]
    
  4. 在提示时输入安全密码。 更多信息请参阅“使用 SSH 密钥密码”

    > Enter passphrase (empty for no passphrase): [Type a passphrase]
    > Enter same passphrase again: [Type passphrase again]
    

将 SSH 密钥添加到 ssh-agent(非必须操作)

将新 SSH 密钥添加到 ssh-agent 以管理密钥之前,应检查现有 SSH 密钥生成新 SSH 密钥

如果已安装 GitHub Desktop,可使用它克隆仓库,而无需处理 SSH 密钥。

  1. 确保 ssh-agent 正在运行。 您可以根据“使用 SSH 密钥密码”中的“自动启动 ssh-agent”说明,或者手动启动它:

    # start the ssh-agent in the background
    $ eval `ssh-agent -s`
    > Agent pid 59566
    
  2. 将 SSH 私钥添加到 ssh-agent。 如果您创建了不同名称的密钥,或者您要添加不同名称的现有密钥,请将命令中的 id_ed25519 替换为您的私钥文件的名称。

    $ ssh-add ~/.ssh/id_ed25519
    

新增 SSH 密钥到 GitHub 帐户

  1. 将 SSH 公钥复制到剪贴板。默认位置在C:\Users\sheng.ssh\id_rsa.pub

  2. 单击个人资料照片 --> Settings(设置) --> SSH and GPG keys(SSH 和 GPG 密钥) --> New SSH key(新 SSH 密钥) -->

    在"Title"(标题)字段中,为新密钥添加描述性标签,将密钥粘贴到 "Key"(密钥)字段

    --> 单击 Add SSH key(添加 SSH 密钥)。--> 如有提示,请确认您的 GitHub 密码

    若是gitee, 复制生成后的 ssh key,通过仓库主页 「管理」->「部署公钥管理」->「添加部署公钥」 ,添加生成的 public key 添加到仓库中。

测试 SSH 连接

  1. 打开 Git Bash。

  2. 输入以下内容:

    $ ssh -T git@github.com
    # Attempts to ssh to GitHub
    # 若是gitee:ssh -T git@gitee.com
    

    您可能会看到类似如下的警告:

    > The authenticity of host 'github.com (IP ADDRESS)' can't be established.
    > RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
    > Are you sure you want to continue connecting (yes/no)?
    
  3. 验证所看到消息中的指纹是否匹配 GitHub 的 RSA 公钥指纹。 如果是,则输入 yes

    > Hi username! You've successfully authenticated, but GitHub does not
    > provide shell access.
    
  4. 验证生成的消息包含您的用户名。 如果收到“权限被拒绝”消息,请参阅“错误:权限被拒绝(公钥)”

将远程 URL 从 HTTPS 切换到 SSH

  1. 打开 Git Bash。

  2. 将当前工作目录更改为您的本地仓库。

  3. 列出现有远程仓库以获取要更改的远程仓库的名称。

    $ git remote -v
    > origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
    > origin  https://github.com/USERNAME/REPOSITORY.git (push)
    
  4. 使用

    git remote set-url
    

    命令将远程的 URL 从 HTTPS 更改为 SSH。

    $ git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
    
  5. 验证远程 URL 是否已更改。

    $ git remote -v
    # Verify new remote URL
    > origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
    > origin  git@github.com:USERNAME/REPOSITORY.git (push)