在這篇文章中:
做過很多遍了,卻總是記不住,這回從頭來描述一下。
介紹
所謂多個git賬號,可能有兩種情況:
- 我有多個github的賬號,不同的賬號對應不同的repo,需要push的時候自動區分賬號
- 我有多個git的賬號,有的是github的,有的是bitbucket的,有的是單位的gitlab的,不同賬號對應不同的repo,需要push的時候自動區分賬號
這兩種情況的處理方法是一樣的,分下面幾步走:
處理
- 先假設我有兩個賬號,一個是github上的,一個是公司gitlab上面的。先為不同的賬號生成不同的ssh-key
ssh-keygen -t rsa -f ~/.ssh/id_rsa_work -c xxx@gmail.com
然后根據提示連續回車即可在~/.ssh目錄下得到id_rsa_work和id_rsa_work.pub兩個文件,id_rsa_work.pub文件里存放的就是我們要使用的keyssh-keygen -t rsa -f ~/.ssh/id_rsa_github -c xxx@gmail.com
然后根據提示連續回車即可在~/.ssh目錄下得到id_rsa_github和id_rsa_github.pub兩個文件,id_rsa_gthub.pub文件里存放的就是我們要使用的key - 把id_rsa_xxx.pub中的key添加到github或gitlab上,這一步在github或gitlab上都有幫助,不再贅述
- 編輯
~/.ssh/config
,設定不同的git 服務器對應不同的key
1 2 3 4 5 6 7 8 9 10 11 12 |
# Default github user(first@mail.com),注意User項直接填git,不用填在github的用戶名 Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa_github # second user(second@mail.com) # 建一個gitlab別名,新建的帳號使用這個別名做克隆和更新 Host 172.16.11.11 HostName 172.16.11.11 User work IdentityFile ~/.ssh/id_rsa_work |
---|
編輯完成后可以使用命令 ssh -vT git@github.com
看看是不是采用了正確的id_rsa_github.pub文件
這樣每次push的時候系統就會根據不同的倉庫地址使用不同的賬號提交了
- 從上面一步可以看到,ssh區分賬號,其實靠的是HostName這個字段,因此如果在github上有多個賬號,很容易的可以把不同的賬號映射到不同的HostName上就可以了。比如我有A和B兩個賬號, 先按照步驟一生成不同的key文件,再修改
~/.ssh/config
內容應該是這樣的。
1 2 3 4 5 6 7 8 9 10 11 12 |
# Default github user(A@mail.com),注意User項直接填git,不用填在github的用戶名 Host A.github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa_github_A # second user(B@mail.com) # 建一個gitlab別名,新建的帳號使用這個別名做克隆和更新 Host A.github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa_github_B |
---|
同時你的github的repo ssh url就要做相應的修改了,比如根據上面的配置,原連接地址是:
git@github.com:testA/gopkg.git
那么根據上面的配置,就要把github.com
換成A.github.com
, 那么ssh解析的時候就會自動把testA.github.com
轉換為 github.com
,修改后就是
git@A.github.com:testA/gopkg.git
直接更改 repo/.git/config
里面的url即可
這樣每次push的時候系統就會根據不同的倉庫地址使用不同的賬號提交了
一些題外話
我有一個repo,想要同時push到不同的倉庫該如何設置?
很簡單, 直接更改 repo/.git/config
里面的url即可,把里面對應tag下的url增加一個就可以了。例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[remote "GitHub"] url = git@github.com:elliottcable/Paws.o.git fetch = +refs/heads/*:refs/remotes/GitHub/* [branch "Master"] remote = GitHub merge = refs/heads/Master [remote "Codaset"] url = git@codaset.com:elliottcable/paws-o.git fetch = +refs/heads/*:refs/remotes/Codaset/* [remote "Paws"] url = git@github.com:Paws/Paws.o.git fetch = +refs/heads/*:refs/remotes/Paws/* [remote "Origin"] url = git@github.com:Paws/Paws.o.git url = git@codaset.com:elliottcable/paws-o.git |
---|
上面這個立即就是有4個遠端倉庫,不同的tag表示不同的遠端倉庫,最后的Origin標簽寫法表示默認push到github和codaset這兩個遠端倉庫去。當然,你可以自己隨意定制tag和url
我有一個github的repo,clone沒有問題,push的時候總是報錯:error: The requested URL returned error: 403 while accessing xxx
這個問題也困擾了我一段時間,后來發現修改 repo/.git/config
里面的url,把https地址替換為ssh就好了。
例如
url=https://MichaelDrogalis@github.com/derekerdmann/lunch_call.git
替換為
url=ssh://git@github.com/derekerdmann/lunch_call.git
本機添加多個git倉庫賬號
我們可能會需要在一台電腦上以不同的github賬戶去使用git,這時就需要去解決如何管理本機上的多個ssh key的問題了。
生成新ssh key
如果我們電腦上已經存在了一個ssh key,那么我們需要在我們電腦上生成第二個你想在本電腦上使用的id_rsa,使用命令:ssh-keygen -t rsa -C "你的github注冊郵箱"
。
下圖紅色標注部分會提示你把新生成的id_rsa存放到哪里,此處默認會存放在c盤的用戶名下的.ssh文件夾下(即你第一個github用戶ssh key存放的目錄),因此我們需要輸入路徑/c/Users/DodoMonster/.ssh(注意此路徑是你的系統盤下用戶目錄安放ssh密鑰的目錄,請使用自己電腦上相對應的目錄),最后我以“id_rsa_me”重新命名了ssh key防止默認與已有的ssh key重復。
在輸入了路徑后,會提示你輸入提交項目時輸入的驗證密碼,不輸則表示不用密碼,這是為了防止別人隨便在你的項目上push東西,所以最好還是輸入以下你的密碼。回車,再重復輸入確認回車即可。
添加新ssh key
默認SSH只會讀取id_rsa,所以為了讓SSH識別新的私鑰,需要將其添加到SSH agent
使用命令:ssh-add ~/.ssh/id_rsa_me
如果報錯:Could not open a connection to your authentication agent.無法連接到ssh agent
可執行ssh-agent bash
命令后再執行ssh-add
命
然后將公鑰添加到git賬號中 https://github.com/settings/keys
配置config文件
查看.ssh文件中是否存在config文件
如果已存在則直接編輯config文件,命令:vim config
#這是linux的命令,進入了vim界面后按a或i或A或I
進入編輯模式,編輯完成后按esc鍵輸入:wq
保存文件退出
如果不存在則需要創建config文件,命令:touch config
,再對config文件進行編輯
對config文件進行配置填寫:
#Default 第一個賬號(123456@xxxx.com) Host default HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_me #second 第二個賬號(38894403@xxxx.com) Host zc HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa
其中Host 后的名字可以隨意方便自己記憶,但HostName必須為github.com(或者其它git地址)。
測試是否配置成功
使用命令:
ssh -T git@zc
出現如圖的歡迎語則為配置成功。
注意:配置完成后,在連接Host不是github.com的github倉庫時,遠程庫的地址要對應地做一些修改:
而並非原來的git@github.com:38894403/react.git
這樣每次連接都會使用id_rsa_me與服務器進行連接。
配置至此,大功告成!
添加SSH密鑰到GitHub
$ clip < ~/.ssh/id_rsa.pub
bash: /c/Users/UsersName/.ssh/id_rsa.pub: No such file or directory
【轉】Generating SSH keys 生成 SSH 密鑰
Step 1、檢查本機現有的SSH密鑰
檢查~/.ssh看看是否有名為d_rsa.pub
和id_dsa.pub的2個文件。
如果你什么都沒得到這些文件,轉到 步驟2 ;否則,請跳到 第3步。
打開你的Git Bash,輸入:
$ ls -al ~/.ssh
Step 2、創建一個新的SSH密鑰
注意期間“輸入密碼(空沒有密碼):再次輸入密碼]:[鍵入密碼]#再次輸入相同的密碼”,如下:
“Enter passphrase (empty for no passphrase): [Type a passphrase] # Enter same passphrase again:”
可不管,直接一路ENTER。
$ ssh-keygen -t rsa -C "注冊Github用的郵箱" $ ssh-keygen -t rsa -C "注冊Github用的郵箱"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/UsersName/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/UsersName/.ssh/id_rsa.
Your public key has been saved in /c/Users/UsersName/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:rwuerTS5wjzt86GtvvVt0jwm5nquIJWpdXt+kw2exYU 注冊Github用的郵箱
The key's randomart image is:
+---[RSA 2048]----+
| |
| |
| . |
| o E .|
| = S . . |
| + o o . o |
| oo.* + o+ * |
| =+o@ *=.% . |
| =@*OO=*.o |
+----[SHA256]-----+
現在你的公鑰已經保存在/c/Users/you/.ssh/id_rsa.pub中。
如果以上“ssh-keygen 生成一個公鑰私鑰”的過程中選擇設置了密碼,及可通過“ssh-add”來實現ssh免密碼登陸。(詳情可參照“ssh-agent用法”)
添加新的SSh密鑰到ssh-keyen中:
$ eval "$ (ssh-agent -s)" #ssh-agent啟動之后,如果通過公鑰做身份驗證,只需第一次輸入密碼,以后ssh-agent會幫你自動輸入。
Step 3、將你的SSH key添加到GitHub
運行以下代碼復制id_rsa.pub到剪切板:
$ clip < ~/.ssh/id_rsa.pub
或
手動復制 ~/.ssh文件夾下的id_rsa.pub。
現在將其添加到GitHub上(參考GitHub官網教程“Adding a new SSH key to your GitHub account”):
- 在頁面的用戶欄的右上角,單擊 Settings ;
- 在左側邊欄點擊 SSH and GPG keys.;
- 點擊 New SSH key ;
- 在Title標題區域中,為新的SSH密鑰添加一個描述性標簽。例如,如果您使用的是個人的PC,您可以調用這個關鍵的“Personal MacBook Air”;
- 粘貼您的鑰匙插入 Key 區域中;
- 點擊 Add SSH key ;
- 確認通過輸入操作GitHub的密碼。
Step 4、測試SSH key是否成功的添加到GitHub
$ ssh -T git@github.com # 用 ssh 連接 github $ ssh -T git@github.com
中途會有如下提示,選擇yes即可:
Are you sure you want to continue connecting (yes/no)? yes