GIT SSH多帳戶配置

一、生成多對公私鑰對應多個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

二、配置全局config

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

# 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

三、ssh測試連通性

#測試ssh到osc的連通性(osc即爲上面配置的host別名) 
ssh -T git@osc

四、取消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

五、修改每一個項目中.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:java

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

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

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

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

http://www.javashuo.com/article/p-zluhakox-ch.htmlssh

相關文章
相關標籤/搜索