Git多账号管理通过ssh公钥的方式,git,gitlab,gitee

136 阅读3分钟

在日常开发中,我们可能会遇到需要在同一台机器上使用多个Git账号的情况,例如同时使用Git、GitLab和Gitee。这种情况下,我们可以通过SSH公钥的方式来管理多个账号。

下面就来详细介绍一下这个过程。

首先,我们需要创建SSH公钥。打开你的命令行工具(如Terminal或者CMD),输入以下命令:

ssh-keygen -t rsa -C "your-email@example.com"

其中,“your-email@example.com”应替换为你自己的邮箱地址。按照提示进行操作,在询问保存位置时可以选择不同路径以区分不同账户。

例如:

ssh-keygen -t rsa -C "git@example.com"  # 为git创建密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa): /home/username/.ssh/id_rsa_git

# 同样地, 我们也可以为 GitLab 和 Gitee 创建密钥:
ssh-keygen -t rsa -C "gitlab@example.com"
Enter file in which to save the key (/home/username/.ssh/id_rsa): /home/username/.ssh/id_rsa_gitlab

# Gitee:
 ssh-keygen -t rsa-C "gitee@example.com"
 Enter file in which to save the key (/home/username/. ssh /id_ RSA): / home/user name /. SSH /id_ RSA_gitee 
​

接着,在生成了SSH公私密匙对后, 我们需要将生成的公钥添加到对应的Git、GitLab、Gitee账户中。公钥文件通常是你设置的文件名加上.pub后缀,例如 id_rsa_git.pub。你可以使用文本编辑器打开它,然后复制内容。

然后打开对应网站的SSH Keys设置页面(通常在账户设置或者安全设置中),将复制的内容粘贴到新建SSH Key中。

接下来,我们需要在本地创建一个SSH配置文件来管理这些密钥。在 ~/.ssh/ 目录下创建一个名为 config 的文件,并编辑它:

# Git
Host git.com
HostName git.com
User git-user-name  # 你在git上注册时使用的用户名或者邮箱地址。
IdentityFile ~/.ssh/id_rsa_git

# GitLab 
Host gitlab.com 
HostName gitlab.com 
User gitlab-user-name # 你在gitlab上注册时使用提供用户名或者邮箱地址。
IdentityFile ~/.ssh/id_rsa_gitlab

# Gitee 
Host gitee.com  
HostName gitee.com  
User gitee-user-name # 你在gitee上注册时提供用户名或者邮箱地址。
IdentityFile ~/.ssh/id_rsa_gitee
​

最后一步是测试我们是否配置成功:

# 测试git:
 ssh -T -ai ~/. ssh /id_ RSA_ GIT GIT@GIT. COM 

# 测试GitLab:
 ssh -T -ai ~ /. SSH /id_ RSA_GitLab GITLAB@GITLAB. COM 

测试GITEESSH-T-AI ~ /. SSH /ID_RSA_GITEE GITEE@GITEE. COM

如果看到类似于 "You've successfully authenticated, but GitHub does not provide shell access." 的消息,那么恭喜你,你已经成功配置了多个Git账户。

以上就是通过SSH公钥的方式管理Git、GitLab、Gitee多账号的方法。希望这个指南能帮助到你。

云服务器推荐

蓝易云国内/海外高防云服务器推荐

蓝易云-五网CN2服务器【点我购买】

蓝易云采用KVM高性能架构,稳定可靠,安全无忧!
蓝易云服务器真实CN2回国线路,不伪造,只做高质量海外服务器。


海外免备案云服务器链接:www.tsyvps.com

蓝易云香港五网CN2 GIA/GT精品网络服务器。拒绝绕路,拒绝不稳定。