git配置多个帐户

前言

公司最近搭建了git服务器,代码要迁移到git服务上。html

奈何以前我本机已经绑定了git帐户,那么如何再添加一个git帐户,而且提交代码时,指定提交到不一样的git服务器呢?git

Step1 取消以前的git全局设置

# 取消全局帐户配置
git config --global --unset user.name
git 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服务进行链接,还须要一些配置,以下github

Host gitosc
HostName git.oschina.net
User iron_will
IdentityFile C:/Users/lixk/.ssh/id_rsa_gitosc

Host github
HostName github.com
User lixaingke
IdentityFile 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

参考

http://www.cnblogs.com/xjnotxj/p/5845574.htmlbash

http://stackoverflow.com/questions/17846529/could-not-open-a-connection-to-your-authentication-agent服务器

相关文章
相关标签/搜索