您的位置:首页 > 其它

GIT SSH多账户配置

2016-06-09 00:00 288 查看
摘要: 在我们日常的开发工作中,会用到多个GIT仓库(公司内部的GIT仓库、github、oschina的git仓库等),这就需要我们配置不同的ssh-key

1、生成多对公私钥对应多个git服务器并将公钥copy到服务器上

ssh-keygen -t rsa -C "chenyh@yunhetong.net" -f id_rsa_work
ssh-keygen -t rsa -C "hzchenyh@163.com" -f id_rsa_osc

2、配置全局config

全局配置文件在~/.ssh/下的config文件中:

# oschina(hzcheyh@163.com)	========================================
Host osc
HostName git.oschina.net
User hzchenyh@163.com
IdentityFile ~/.ssh/id_rsa_osc
# yunhetong(chenyh@yunhetong.net)	========================================
Host yunhetong
HostName 120.20.72.179
Port 7999
User git
IdentityFile ~/.ssh/id_rsa_work

3、ssh测试连通性

#测试ssh到osc的连通性(osc即为上面配置的host别名)
ssh -T git@osc

4、取消git全局name和email配置,并为每个项目设置独立的name和email

#取消git全局config
git config --global --unset user.name
git config --global --unset user.email

#(公司项目)进入项目目录下,为每个项目设置name和email
git config user.name 醉公子
git config user.email chenyh@yunhetong.net

#(自己项目)进入项目目录下,为每个项目设置name和email
git config user.name 醉公子osc
git config user.email hzchenyh@163.com

5、修改每个项目中.git目录下的config文件的配置

[core]
......
[remote "origin"]
#url = ssh://git@120.20.72.179:7999/chenyh/user.git    此为原始URL,经上面的host替换后如下所示
url = ssh://git@yunhetong/chenyh/user.git
fetch = +refs/heads/*:refs/remotes/origin/*
[user]
name = 醉公子
email = chenyh@yunhetong.net

[core]
......
[remote "origin"]
#url = git@git.oschina.net:hzchenyh/jfinal.git
url = git@osc:hzchenyh/jfinalBlog.git
fetch = +refs/heads/*:refs/remotes/origin/*

# 以下表示本地默认分支master和远程分支master的一个对应关系
[branch "master"]
remote = origin
merge = refs/heads/master
[user]
name = 醉公子osc
email = hzchenyh@163.com

ref:

https://gist.github.com/suziewong/4378434

http://www.liaohuqiu.net/cn/posts/using-diffrent-user-config-for-different-repository/

http://www.liaohuqiu.net/cn/posts/git-setup-and-setting/

http://www.blogjava.net/lishunli/archive/2012/03/08/371556.html

https://segmentfault.com/a/1190000002994742
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  git 多账户 ssh