使用ssh连接到github,避免每次上传代码要输入账号密码。


Github官方文档

参考文档:使用 SSH 连接到 GitHub
使用 SSH 连接到 GitHub
您可以使用 SSH 连接到 GitHub。

关于 SSH

使用 SSH 协议可以连接远程服务器和服务并向它们验证。 利用 SSH 密钥可以连接 GitHub,而无需在每次访问时提供用户名或密码。

检查现有 SSH 密钥

在生成 SSH 密钥之前,您可以检查是否有任何现有的 SSH 密钥。

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

检查现有 SSH 密钥后,您可以生成新 SSH 密钥以用于身份验证,然后将其添加到 ssh-agent。

生成新 SSH 密钥

  1. 打开 Terminal(终端)。

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

    $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

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

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

    > Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [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 密钥。 将 SSH 密钥添加到该代理时,应使用默认的 macOS ssh-add 命令,而不是使用通过 macports, homebrew 或某些其他外部来源安装的应用程序。

  1. 在后台启动 ssh 代理。
    $ eval "$(ssh-agent -s)"
    > Agent pid 59566
  2. 如果您使用的是 macOS Sierra 10.12.2 或更高版本,则需要修改 ~/.ssh/config 文件以自动将密钥加载到 ssh-agent 中并在密钥链中存储密码。
    Host *
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/id_rsa
  3. 将 SSH 私钥添加到 ssh-agent 并将密码存储在密钥链中。 如果您创建了不同名称的密钥,或者您要添加不同名称的现有密钥,请将命令中的 id_rsa 替换为您的私钥文件的名称。
    $ ssh-add -K ~/.ssh/id_rsa
  • 注:-K 选项位于 Apple 的 ssh-add 标准版本中,当您将 ssh 密钥添加到 ssh-agent 时,它会将密码存储在您的密钥链中。

新增 SSH 密钥到 GitHub 帐户

要配置 GitHub 帐户使用新的(或现有)SSH 密钥,您还需要将其添加到 GitHub 帐户。

  1. 将 SSH 密钥复制到剪贴板。

如果您的 SSH 密钥文件与示例代码不同,请修改文件名以匹配您当前的设置。 在复制密钥时,请勿添加任何新行或空格。

$ pbcopy < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard
  • 提示:如果 pbcopy 不可用,可找到隐藏的 .ssh 文件夹,在常用的文本编辑器中打开该文件,并将其复制到剪贴板。
  1. 在任何页面的右上角,单击您的个人资料照片,然后单击 Settings(设置)。

用户栏中的 Settings 图标
3.在用户设置侧边栏中,单击 SSH and GPG keys(SSH 和 GPG 密钥)。

身份验证密钥
4. 单击 New SSH key(新 SSH 密钥)或 Add SSH key(添加 SSH 密钥)。

SSH 密钥按钮
5. 在 “Title”(标题)字段中,为新密钥添加描述性标签。 例如,如果您使用的是个人 Mac,此密钥名称可能是 “Personal MacBook Air”。

  1. 将密钥粘贴到 “Key”(密钥)字段。

密钥字段
7. 单击 Add SSH key(添加 SSH 密钥)。

添加密钥按钮
8. 如有提示,请确认您的 GitHub 密码。

测试 SSH 连接→

设置 SSH 密钥并将其添加到您的 GitHub 帐户后,您可以测试连接。

  1. 打开 Terminal(终端)。

  2. 输入以下内容:

    $ ssh -T git@github.com

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

    > The authenticity of host 'github.com (IP ADDRESS)' can't be established.
    > RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
    > Are you sure you want to continue connecting (yes/no)?

    或类似如下:

    > 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. 验证您看到的消息中的指纹匹配步骤 2 中的消息之一,然后输入 yes:

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

使用 SSH 密钥密码→

您可以保护 SSH 密钥并配置身份验证代理,这样您就不必在每次使用 SSH 密钥时重新输入密码。

  1. 添加或更改密码
    通过输入以下命令,您可以更改现有私钥的密码而无需重新生成密钥对:

    $ ssh-keygen -p
    # Start the SSH key creation process
    > Enter file in which the key is (/Users/you/.ssh/id_rsa): [Hit enter]
    > Key has comment '/Users/you/.ssh/id_rsa'
    > Enter new passphrase (empty for no passphrase): [Type new passphrase]
    > Enter same passphrase again: [One more time for luck]
    > Your identification has been saved with the new passphrase.

    如果您的密钥已有密码,系统将提示您输入该密码,然后才能更改为新密码。

  2. 在密钥链中保存密码
    在 OS X Leopard 上通过 OS X El Capitan,这些默认私钥文件将自动处理:

  • .ssh/id_rsa
  • .ssh/identity
    初次使用密钥时,系统将提示您输入密码。 如果选择使用密钥链保存密码,则无需再次输入密码。

否则,您可在将密钥添加到 ssh-agent 时在密钥链中存储密码。


简易版

新建ssh

ssh-keygen -t rsa -C "邮箱@xx.com"

此处可以不是邮箱,只是用于识别区别使用。
提示输入保存密钥路径,直接回车即可(三次默认回车)。
第一次提醒
设置密钥储存位置及名称
第二次提示密码(为了避免麻烦,每次使用要输入密码。就设置为空,直接回车)
第三次提示 重复密码

设置github密钥

/.ssh,文件夹内会存在2个文件:id_rsa和id_rsa.pub

打开id_rsa.pub,将里面的内容复制到github的ssh设置中
将公钥内容添加到GitHub

 1、打开GitHub网站进行登陆;

 2、到个人设置页Personal Settings;

 3、找到SSH and GPG keys;

 4、选择新建SSH key:new ssh key;

 5、填写和粘贴公钥内容(不含中文)。

修改全局git的用户名和邮箱

git config --global user.name "用户名" 
git config --global user.email 邮箱@xx.com

测试

ssh -T git@github.com

对 GitHub 尝试 ssh