通過SSH免密提交代碼到github,屢用不爽啊,本人很喜歡。
本人兩個github賬號count A和count B,分別記錄不同的東西,先前一臺電腦上只使用一個賬號 count A,並沒有發現什麼問題。
問題:
但是最近準備也同時在本電腦上使用另一個count B賬號,在添加公鑰到github時,提示"Key is already in use",原因是這個電腦上的公鑰已經配置到count A了, 而github是不允許一個公鑰同時添加到多個賬戶的,不然github服務器檢測到有多個公鑰和你的私鑰匹配,你的每次提交豈不是要被提交到多個賬號或錯誤賬號對應的倉庫中了(Oh My God,老闆很生氣,後果很嚴重)
查了網上基本上都是如下兩種相關方案
1,刪除count A中配置的公鑰,這樣就可以將公鑰留出來配置到count B中(這個遇到與我一樣的問題,但是刪除了count A中的公鑰,我以後還如何往count A中提交代碼,總不能刪來刪去,加來加去吧)
2,另一種是git多賬號配置,但是點進去一看,大多是爲不同的倉庫託管平臺github、gitlab、gitee進行配置, 提交代碼到這些不同平臺的倉庫(此種與其說是多賬號,倒不如說是多平臺配置嘛,且同時在這不同的平臺上使用同一把公鑰也是允許的,更不會報"Key is already in use", 些方案也解決不了我的問題)
那麼有沒有一種方案夠滿足在同一臺電腦上同時使用不同類型的git代碼託管平臺、同一平臺下不同的賬號呢?
答案:沒有..........不可能,自然是有的了,以下進入最正點的解決方案部分:
解決方案:
首先爲了便於密鑰的個性化管理,在本機用戶的.ssh下爲不同平臺和不同用戶創建不同的密鑰文件夾:
1,github-552277(該文件夾用來保存github上的用戶名爲552277賬號密鑰
2,github-dailyplanner(該文件夾用來保存github上用戶名爲dailyplanner賬號密鑰)
3,gitee-dailyplanner(該文件夾用來保存gitee上用戶名爲dailyplanner賬號密鑰)
並分別在各個文件夾下按如下方式生成一對密鑰:
並分別在各個文件夾下按如下方式生成一對密鑰:
$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/weichangzhong/.ssh/id_rsa): /c/Users/weichangzhong/.ssh/github-552277/id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/weichangzhong/.ssh/github-552277/id_rsa.
Your public key has been saved in /c/Users/weichangzhong/.ssh/github-552277/id_rsa.pub.
The key fingerprint is:
SHA256:PJuK5kRDsZRgOJGKdp5NJtUPvdwGnJUIxNefD0/Bj9I [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|.+o.o.+o+ =.. . |
|+. ..+ + B o o |
|o. + = + ....o|
|o. + o . + o.+E..|
|. o O S . .= |
| + o + o |
| . o |
| ... . |
| oo . |
+----[SHA256]-----+
然後在.ssh下創建ssh的無後綴名的配置文件config,使git命令執行時能夠根據該文件的配置信息獲取不同的密鑰,
config:
Host github-552277.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github-552277/id_rsa
User git
Host github-dailyplanner.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github-dailyplanner/id_rsa
User git
Host gitee-dailyplanner.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee-dailyplanner/id_rsa
User git
其中
1)Host後面的主機別名隨意取,但是後續初始化本地跟蹤遠程項目的倉庫時會用到
2)HostName後面的主機名,要和所要配置密鑰的git託管平臺的域名主體部分一致
3)IdentityFile 後面是相應平臺密鑰文件所在路徑+密鑰文件名稱
注意: 同一個主機別名只能配置一個密鑰,如果多個HOST 主機別名相同,則只有第一個生效。
最後使用各個密鑰的流程如下:
1,向gitee 552277賬號中新建的倉庫github-527-rep提交代碼:
正常的流程應該是如下
echo "# github-527-rep" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin [email protected]:552277/github-527-rep.git
git push -u origin master
而我們變更了github的主機別名後,則需要將其中命令:
git remote add origin [email protected]:552277/github-527-rep.git
變更爲如下即可
git remote add origin [email protected]:552277/github-527-rep.git
其中gitee-552277.com就是我們在.ssh/config中配置的別名,當我們執行如下命令時, git會自動識別爲通過SSH協議連接遠程倉庫,並在用戶的.ssh/config中找到名稱爲github-552277的配置信息:
Host github-552277.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github-552277/id_rsa
User git
然後根據配置信息知道要從~/.ssh/github-552277/id_rsa文件中獲取密鑰,鏈接到github.com託管平臺的服務,找到552277用戶的github-527-rep倉庫進行關聯,並在本地當前項目隱藏目錄.git/config下生成關聯的遠程倉庫信息。
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "origin"]
url = [email protected]:552277/github-527-rep.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
第一次執行帶分支另名的命令後,以後每次執行與遠程倉庫相關的命令時,不必再在命令行中添加git遠程倉庫信息,每次都從配置文件中獲取相應的 遠程倉庫信息和密鑰,然後再進行連接並執行相關操作
注意,如果是克隆原來已有的分支到本地,也要將對應的克隆命令:
git clone [email protected]:552277/github-527-rep.git
改爲:
git clone [email protected]:552277/github-527-rep.git
即凡是直接用到[email protected]:username/rep.git的,都要做相應的改動。
2,同理,如果第一次執行與github dailyplanner賬號下的github-dailyplanner-rep倉庫相關操作時,則只需要將其中
[email protected]:dailyplanner/github-dailyplanner-rep.git
命令改爲如下即可:
[email protected]:dailyplanner/github-dailyplanner-rep.git
3,同理,如果第一次執行與gitee平臺的dailyplanner賬號下的gitee-daily-rep倉庫相關操作時,則只需要將命令
[email protected]:dailyplanner/gitee-daily-rep.git
變更爲如下即可
[email protected]:dailyplanner/gitee-daily-rep.git
4,如果執行[email protected]:dailyplanner/gitlab-dailyplanner-rep.git相關命令時............
......what ......竟然在.ssh/config中找不到相關配置了......
不過這時它纔會使用默認的配置,就是使用.ssh/id_rsa密鑰,連接到gitlab託管服務,找到dailyplanner用戶的github-dailyplanner-rep倉庫進行關聯,並在本地當前項目隱藏目錄.git/config下生成關聯的遠程倉庫信息。
如上方案,即可實現在同一臺電腦上使用同一個或不同的密鑰連接不同的代碼託管平臺 或 使用不同的密鑰連接同一類代碼託管平臺中的不同賬號