Git - 使用多个帐号

背景:在工作中,都会有一个工作的Git帐号(公司Gitlab),而空闲时间做的个人东西又想放进Github里面,这时候就需要配置两个帐号和服务器。假设之前已经配置好了工作的帐号,打开Git bash:

1、创建个人的SSH key:

#新建SSH key:
$ cd ~/.ssh     # 切换到C:\Users\Administrator\.ssh
ssh-keygen -t rsa -C "youremail@example.com"  # 新建工作的SSH key
# 设置名称为id_rsa_hason(名字随意)
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa_hason

2、添加新密钥到SSH agent:

因为默认只读取id_rsa,为了让SSH识别新的私钥,需将其添加到SSH agent中:

ssh-add ~/.ssh/id_rsa_hason

注意:如果出现Could not open a connection to your authentication agent,参考底部详情

3、修改config文件

若~/.ssh/目录下不存在config文件,则新建一个,内容写上:

# 该配置用于工作
# Host 服务器别名
Host 192.168.2.36
# HostName 服务器ip地址或机器名
HostName 192.168.2.36
# User连接服务器的用户名
User huanghs
# IdentityFile 密匙文件的具体路径
IdentityFile C:/Users/P/.ssh/id_rsa


# 该配置用于个人 github 上
# Host 服务器别名
Host github.com
# HostName 服务器ip地址或机器名
HostName github.com
# User连接服务器的用户名
User hasonHuang
# IdentityFile 密匙文件的具体路径
IdentityFile C:/Users/P/.ssh/id_rsa_hason

4、添加新密钥到Github

把~/.ssh/id_rsa_hason.pub的内容添加到Github的SSH keys中


5、测试

使用ssh -T git@Host进行测试,其中Host指上面配置的服务器别名

ssh -T git@github.com


6、大功告成!



常见问题:

1、出现Could not open a connection to your authentication agent

3种解决方法:

(a) 先输入ssh-agent bash,然后再输入ssh-add ~/.ssh/id_rsa_hason;

(b)先输入eval $(ssh-agent),然后输入ssh-add ~/.ssh/id_rsa_hason;

(c)使用Git GUI生成密钥,密钥会自动被加进ssh-agent中;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值