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服務器

相關文章
相關標籤/搜索