本文是一篇筆記, 內(nèi)容經(jīng)過一些修改, 很抱歉原文鏈接找不到了。如有侵權(quán)請告知刪除。這是其中一處鏈接 一個客戶端設(shè)置多個github賬號 和 如何同一臺電腦配置多個git或github賬號
一個客戶端設(shè)置兩個 github 賬號
需求
需要在一臺電腦上同時使用兩個不同的 github 賬號,負責(zé)不同的用途。(例如一個用于技術(shù), 一個用于非技術(shù))
前期工作
至少有兩個 github 賬號 (假設(shè)有兩個賬號 一個為 ohmyone ,另一個為 ohmytwo )
取消 git 全局設(shè)置
git config --global --unset user.name
git config --global --unset user.email
SSH 配置
生成 id_rsa 私鑰 和 id_rsa.pub 公鑰。
ohmyone 可以直接回車,默認生成 id_rsa 和 id_rsa.pub。
ssh-keygen -t rsa -C "ohmyone@sina.com"
但是 ohmytwo 會出現(xiàn)提示輸入文件名,輸入與默認配置不一樣的文件名,比如: id_rsa_ohmytwo。
cd ~/.ssh
ssh-keygen -t rsa -C "ohmytwo@outlook.com" # 輸入文件名, 但是別輸密碼
github 添加公鑰 id_rsa.pub 和 id_rsa_two.pub。
分別登陸 ohmyone , ohmytwo 的賬號,在 Account Settings 的 SSH Keys 里,點 Add SSH Keys ,將公鑰(.pub文件)中的內(nèi)容粘貼到”Key”中,并輸入”Title”.
添加 ssh key
Windows 上推薦使用 GitBash 命令行工具:
eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_ohmytwo
可以在添加前使用下面命令刪除所有的 key
ssh-add -D
最后可以通過下面命令,查看key的設(shè)置
ssh-add -l
修改 ssh config 文件
cd ~/.ssh/touch config
打開 .ssh 文件夾下的 config 文件,進行配置
# default
Host github.com
HostName github.com
User ohmyone
IdentityFile ~/.ssh/id_rsa
# ohmytwo
Host ohmytwo.github.com # 前綴名可以任意設(shè)置
HostName github.com
User ohmytwo
IdentityFile ~/.ssh/id_rsa_two
這里必須采用這樣的方式設(shè)置,否則 push 時會出現(xiàn)以下錯誤:
ERROR: Permission to ohmytwo/ohmytwo.github.com.git denied to ohmyone.
簡單分析下原因,我們可以發(fā)現(xiàn) ssh 客戶端是通過類似:
git@github.com:ohmyone/ohmyone.github.com.git
這樣的 git 地址中的 User 和 Host 來識別使用哪個本地私鑰的。
很明顯,如果 User 和 Host 始終為 git 和 github.com,那么就只能使用一個私鑰。
所以需要上面的方式配置,每個賬號使用了自己的 Host,每個 Host 的域名做 CNAME 解析到 github.com,這樣 ssh 在連接時就可以區(qū)別不同的賬號了。
測試 SSH 連接
ssh -T git@github.com # 測試 ohmyone ssh 連接
#Hi ohmyone! You've successfully authenticated, but GitHub does not provide shell access.
ssh -T git@ohmytwo.github.com # 測試 ohmytwo ssh 連接
#Hi ohmytwo! You've successfully authenticated, but GitHub does not provide shell access.
但是這樣還沒有完,下面還有關(guān)聯(lián)的設(shè)置。
用戶名和郵箱的局部配置
新建 git 項目或者 clone 已有的項目??梢杂?git init 或者 git clone 創(chuàng)建本地項目。
分別在 ohmyone 和 ohmytwo 的git項目目錄下,使用下面的命令設(shè)置賬號關(guān)聯(lián)
git config user.name ohmyone
git config user.email ohmyone@sina.com
git config user.name ohmytwo
git config user.email ohmytwo@outlook.com
即在各自的 git 項目文件夾中設(shè)置局部 user 和 email, 執(zhí)行上面的 git 命令。
查看git項目的配置
git config --list
查看 ohmyone 的remote.origin.url= git@github.com:ohmyone/ohmyone.github.com.git
查看 ohmytwo 的remote.origin.url= git@github.com:ohmytwo/ohmytwo.github.com.git
修改遠端倉庫
由于 ohmyone 使用的是默認的 Host,所以不需要修改,但是 ohmytwo 使用的 Host 是 ohmytwo.github.com ,則需要進行修改
git remote rm origin
git remote add origin git@ohmytwo.github.com:ohmytwo/ohmytwo.github.com.git
上傳更改。上面所有的設(shè)置無誤后,可以修改代碼,然后上傳了。
提交
git add -A
git commit -m "your comments"
git push
如果遇到warning
warning: push.default is unset; its implicit value is changing in Git 2.0 from ‘matching’ to ‘simple’. To squelch this messageand maintain the current behavior after the default changes, use…
推薦使用下面命令設(shè)置。
git config --global push.default simple
參考
http://blog.csdn.net/wzy_1988/article/details/19967465
http://testerhome.com/topics/752
http://hily.me/blog/2013/05/github-multiple-account-and-multiple-repository/