一台电脑同时使用多个Git账号

需求

一台电脑同时使用多个Git账号,互相不干扰。

思路

  1. 生成多个 ssh key
  2. 针对不同的项目使用不同的 ssh key

解决方案

下面以两个Git(gitee.com)账号( account_1@qq.comaccount_2@qq.com)进行演示

约定:

$开始的代码表示用户输入的内容

${} 表示引用变量,请以实际值为准

1. 创建并进入~/.ssh 目录

# 创建 .ssh 目录(如果报文件夹已存在的错误,忽略即可)
$ mkdir ~/.ssh
# 进入 .ssh 目录
$ cd ~/.ssh

2. 生成多个 ssh key

2.1. 操作步骤

# 需要管理几个账号就生成几次,请使用实际邮箱地址替换 ${emailAddress}
$ ssh-keygen -t rsa -C "${emailAddress}"
# 提示输入 id_rsa 文件的保存位置
Enter file in which to save the key (.../.ssh/id_rsa):
$ ${emailName}
# 提示输入密码(直接回车)
Enter passphrase (empty for no passphrase):
$
# 提示再次输入密码(再次回车)
Enter same passphrase again:
$

2.2. 实际操作演示

# 1. 生成 account_1@qq.com 的 ssh key
$ ssh-keygen -t rsa -C "account_1@qq.com"
Enter file in which to save the key (.../.ssh/id_rsa):
$ account_1
Enter passphrase (empty for no passphrase):
$
Enter same passphrase again:
$

# 2. 生成 account_2@qq.com 的 ssh key
$ ssh-keygen -t rsa -C "account_2@qq.com"
Enter file in which to save the key (.../.ssh/id_rsa):
$ account_2
Enter passphrase (empty for no passphrase):
$
Enter same passphrase again:
$

3. 创建并配置 config 文件

3.1. 创建 config 文件

文件在 ~/.ssh

# 创建 config 文件夹
$ touch ~/.ssh/config

3.2. 编辑 ~/.ssh/config 文件

向其中加入如下内容:

# account_1(account_1@qq.com)
        Host account_1.gitee.com
        HostName gitee.com
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/account_1
        User account_1
        
# account_2(account_2@qq.com)
        Host account_2.gitee.com
        HostName gitee.com
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/account_2
        User account_2

4. 部署 ssh key

参见其他部署 ssh key 的教程

5. 使用


# 原命令
$ git clone git@gitee.com:${username}/${projectName}.git
# 新命令(假如是 account_1@qq.com 的项目)
$ git clone git@account_1.gitee.com:${username}/${projectName}.git

转载于:https://my.oschina.net/u/3578363/blog/2209781

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值