前言
公司最近搭建了git服务器,代码要迁移到git服务上。
奈何之前我本机已经绑定了git账户,那么如何再添加一个git账户,并且提交代码时,指定提交到不同的git服务器呢?
Step1 取消之前的git全局设置
# 取消全局账户配置git config --global --unset user.namegit config --global --unset user.email# 增加局部账户配置git config user.name "iron_will"git config user.email "iam_leexk@163.com"
Step2 生成新账户的ssh-key
ssh-keygen -t rsa -C "iam_leexk@163.com"Generating public/private rsa key pair.Enter file in which to save the key (/c/Users/lixk/.ssh/id_rsa): #输入id_rsa_gitosc,会生成两个文件:id_rsa_gitosc,id_rsa_gitosc.pub#在码云上添加SSH公钥id_rsa_gitosc.pub(http://git.oschina.net/profile/sshkeys)
Step3 添加密钥到SSH agent
cd /c/Users/lixk/.ssh/ssh-add id_rsa_gitosc# 如果出现Could not open a connection to your authentication agent的错误,# 执行eval `ssh-agent -s`
Step4 添加config文件
这个时候git客户端已经能与git服务端通过ssh建立连接了,但具体使用哪个账户和哪个git服务进行连接,还需要一些配置,如下
Host gitoscHostName git.oschina.netUser iron_willIdentityFile C:/Users/lixk/.ssh/id_rsa_gitoscHost githubHostName github.comUser lixaingkeIdentityFile C:/Users/lixk/.ssh/id_rsa_github
Step5 使用方法
# 原来的写法git clone git@git.oschina.net:iron_will/myGit.git# 现在的写法git clone git@gitosc:iron_will/myGit.git# 即gitosc和config文件中Host一致,那么实际请求的还是git.oschina.net# 同理,如果你需要克隆github的项目,原来的写法:git clone git@github.com:leexk/ironwill.git# 现在的写法:git clone git@github:leexk/ironwill.git