欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

一台电脑配置多个git账户 gitlab/github/coding

最编程 2024-08-01 21:54:23
...

生成多个公钥

打开命令行终端输入 ssh-keygen -t rsa -C "your_email@example.com"( 你的邮箱)

提示是否使用新的文件名,如果不输入新的文件名,则生成id_rsa文件。
以下两个直接回车即可

Enter passphrase (empty for no passphrase):
Enter same passphrase again:
 //此处点击 Enter 键即可,也可以填写密码,填写密码后每次使用 SSH 方式推送代码时都会要求输入密码,由于这个 Key 也不是用于军事目的,所以也无需设置密码。

成功后返回如下信息

Your identification has been saved in /Users/you/.ssh/id_rsa.
# Your public key has been saved in /Users/you/.ssh/id_rsa.pub.
# The key fingerprint is:
# 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com

复制id_rsa到对应的平台的公钥处,如在coding上是如下


公钥

同理创建一个github和gitlab的rsa文件(当然你也可以再创建多个github、gitlab、coding的rsa,只要区别rsa名称即可),将公钥放在对应平台上。

mac看这里

进入~/.ssh目录,touch config,然后vi config

说明:
Host 为域名别名
Hostname:真实地址
User:用户名
IdentityFile:rsa存放地址

   #github
   Host github1
   Hostname github.com
   User yourname
   IdentityFile ~\.ssh\id_github1_rsa
   #github
   Host github2
   Hostname github.com
   User yourname
   IdentityFile ~\.ssh\id_github2_rsa
   #coding
   Host coding
   Hostname e.coding.net
   User yourname
   IdentityFile ~\.ssh\id_codding_rsa

windows看这里

   #github
   Host github1
   Hostname github.com
   User yourname
   IdentityFile C:\Users\Administrator\.ssh\id_github1_rsa
   #github
   Host github2
   Hostname github.com
   User yourname
   IdentityFile C:\Users\Administrator\.ssh\id_github2_rsa
   #coding
   Host coding
   Hostname e.coding.net
   User yourname
   IdentityFile C:\Users\Administrator\.ssh\id_codding_rsa

测试配置是否生效

进入terminal(windows下cmd进入命令行)

ssh -T git@github1
Hi your name one in github ! You've successfully authenticated, but GitHub does not provide shell access.
ssh -T git@github2
Hi your name one in github ! You've successfully authenticated, but GitHub does not provide shell access.
ssh -T git@coding
Coding 提示: Hello yourname, You've connected to Coding.net via SSH. This is a personal key.
XXX,你好,你已经通过 SSH 协议认证 Coding.net 服务,这是一个个人公钥

clone项目到本地

克隆github1上的项目

git clone git@github1:gzkj/zkjy_ht.git //git clone git@域名别称:用户名/项目名.git

克隆github2上的项目

git clone git@github2:gzkj/zkjy_ht.git //git clone git@域名别称:用户名/项目名.git

克隆coding上的项目

git clone git@coding:gzkj/zkjy_ht.git //git clone git@域名别称:用户名/项目名.git