如何在同一臺電腦上同時使用多個Git賬號?
閱讀本文大概需要 2 分鐘。
來自:?juejin.im/post/6844904066900819982
思 路
同時管理多個SSH key。
解決方案
生成多個SSH key
這里使用one、two兩個賬戶進行舉例。
注意:?在生成多個SSH key的時候一定要在~/.ssh目錄下進行,否則生成的SSH key不會在~/.ssh目錄下,所以以下有操作都是在~/.ssh目錄下進行的。在生成之前盡量刪除此目錄下的所有文件再進行,以免出現(xiàn)不必要的問題。
ssh-keygen?-t?rsa?-C?"[email protected]"
ssh-keygen?-t?rsa?-C?"[email protected]"
復制代碼再輸入命令行的時候在第一次提示Enter file in which to save the key的時候?qū)?code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.047);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(255, 93, 108);box-sizing: border-box !important;overflow-wrap: break-word !important;">ssh文件進行重命名(idrsaone和idrsatwo),這樣就會生成如下目錄中的四個文件。

即兩份包含私鑰和公鑰的4個文件。
獲取密鑰
cat?~/.ssh/id_rsa_one.pub
cat?~/.ssh/id_rsa_two.pub
其中idrsaone.pub和idrsatwo.pub就是上面對ssh文件重命名的文件名。
有了這個密鑰,你就可以將其添加到你所需要用的平臺上去。
創(chuàng)建config文件
在~/.ssh目錄下創(chuàng)建一個config文件
touch?config
這樣就會在~/.ssh目錄下生成一個空的config文件,然后我們在文件中添加以下內(nèi)容:
#?git?server?one
Host?one.aliyun.com?#別名
Hostname?code.aliyun.com?#真實域名
PreferredAuthentications?publickey
IdentityFile?~/.ssh/id_rsa_one?#ssh?文件路徑
User?one
#git?server?two
Host?two.aliyun.com
Hostname?code.aliyun.com
PreferredAuthentications?publickey
IdentityFile?~/.ssh/id_rsa_two
User?two
遠程測試
ssh?–T?one.aliyun.com
ssh?–T?two.aliyun.com
使 用
比如clone到本地
原來的寫法:
[email protected]:項目路徑.git
現(xiàn)在的寫法:
[email protected]:項目路徑.git
[email protected]:項目路徑.git
給倉庫設置局部用戶名和郵箱
git?config?user.name?"one_name";?git?config?user.email?"one_email"
git?config?user.name?"two_name";?git?config?user.email?"two_email"

推薦閱讀:
厲害了!華為將發(fā)布國產(chǎn)編程語言,打破國外壟斷!
微信掃描二維碼,關注我的公眾號
朕已閱?

