前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用 SSH 连接到 GitHub(多帐号)

使用 SSH 连接到 GitHub(多帐号)

作者头像
莲花海
发布2020-01-21 11:00:16
1.4K0
发布2020-01-21 11:00:16
举报
github-ssh.png
github-ssh.png

◎ 通过 SSH 克隆仓库

我们克隆 GitHub 上的仓库时,一般是通过默认的 HTTPS 的方式,而非上面这种 SSH 的方式。对于克隆仓库的话,我们使用起来是感受不到这两种方式的差别的。但是,当我们要将本地的改动提交到 GitHub 的时候,使用 SSH 就会比使用 HTTPS 方便很多——我们无需每次输入用户名和密码?...

单帐号

1

$ ssh-keygen -t rsa -b 4096 -C "reuixiy@gmail.com"

怎样使用 SSH 呢?首先,你需要通过以上命令生成 SSH 密钥对。当然,将上面的邮箱地址改为你自己在 GitHub 上的邮箱地址。然后,如果你是第一次生成的话,一路回车即可(口令 passphrase 非必须)。

之后,cat ~/.ssh/id_rsa.pub 查看生成的公钥并复制,再去 GitHub 上设置一个 New SSH key,标题随便起一个(比如:我的电脑?),然后粘贴公钥即可。

最后,如果你 git clonegit remote add 的是 HTTPS 的地址[1],那么要修改仓库的远程仓库链接地址为 SSH 地址,比如:

1

~/hugo-theme-meme $ git remote set-url origin git@github.com:reuixiy/hugo-theme-meme.git

此外,当你本地第一次连接 GitHub 的服务器时,可能会有警告信息,输入 yes 回车即可。

多帐号

1 2 3 4 5 6 7 8 9

# 帐号一 $ ssh-keygen -t rsa -b 4096 -C "reuixiy@gmail.com" Generating public/private rsa key pair. Enter file in which to save the key (/home/archie/.ssh/id_rsa): /home/archie/.ssh/id_rsa_reuixiy # 帐号二 $ ssh-keygen -t rsa -b 4096 -C "yixiuer.me@gmail.com" Generating public/private rsa key pair. Enter file in which to save the key (/home/archie/.ssh/id_rsa): /home/archie/.ssh/id_rsa_yixiuer

特别注意一下,这里你可不要直接默认回车了,当你看到以下提示时:

1

Enter file in which to save the key (/home/archie/.ssh/id_rsa):

注意修改一下默认的 id_rsa,建议在后面加上你的 GitHub 用户名,比如修改为 id_rsa_reuixiy,见我上面的操作。

然后,我们要将生成的两个密钥添加到 ssh-agent:

1 2 3

$ eval "$(ssh-agent -s)" $ ssh-add ~/.ssh/id_rsa_reuixiy $ ssh-add ~/.ssh/id_rsa_yixiuer

接下来,添加一个 config 文件配置下 SSH:

1

$ vim ~/.ssh/config

输入以下内容(自行修改 hostIdentityFile):

1 2 3 4 5 6 7 8 9

host reuixiy.github.com Hostname github.com User git IdentityFile ~/.ssh/id_rsa_reuixiy host yixiuer.github.com Hostname github.com User git IdentityFile ~/.ssh/id_rsa_yixiuer

类似,修改下相应仓库的远程仓库链接地址,比如:

1 2 3 4 5

# 帐号一 ~/hugo-theme-meme $ git remote set-url origin git@reuixiy.github.com:reuixiy/hugo-theme-meme.git # 帐号二 ~/yixiuer.me $ git remote set-url origin git@yixiuer.github.com:yixiuer/yixiuer.me.git

特别注意:主机名分别是 reuixiy.github.comyixiuer.github.com,而不再是默认的 github.com 了,以后克隆仓库时也要注意,修改为帐号的相应主机名。

最后,将相应的公钥添加到你的相应 GitHub 帐号,成功!???

参考

  1. Connecting to GitHub with SSH | GitHub Help
  2. Use multiple ssh-keys for different GitHub accounts on the same computer | Medium
  3. Git 多账号配置 | 蒋士正的博客

  1. 如果你不记得了,可以查看一下仓库下的 .git 文件夹下的 config 文件确认一下。
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 单帐号
  • 多帐号
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档