下面我們一起來看看關於Github多賬戶多專案管理配置教程,希望此教程對各位同學會有所協助。
在 Github 建立了多個倉庫,並為每個倉庫使用了獨立的 deploy key,結果在進行 git push 操作時,提示沒有許可權。錯誤資訊類似:
ERROR: Permission to quyun/php-backend.git denied to quyun/aliyun-api-tools
官方的協助中有提到這個錯誤:
https://help.github.com/articles/error-permission-to-user-repo-denied-to-user-other-repo
不過對於許多應用情境,使用一個全域的 key 並不能滿足要求。
簡單分析下,我們可以發現 ssh 用戶端是通過類似:
git@github.com:quyun/aliyun-api-tools.git
這樣的 git 地址中的 user 和 host 來識別使用哪個本地私密金鑰的。
很明顯,如果 user 和 host 始終為 git 和 github.com,那麼就只能使用一個私密金鑰。
指定私密金鑰的方式也很簡單,編輯 ~/.ssh/config,添加類似如下配置即可:
Host php-backend.github.quyun.net
IdentityFile ~/.ssh/github-php-backend
User git
Host aliyun-api-tools.github.quyun.net
IdentityFile ~/.ssh/github-aliyun-api-tools
User git
配置格式很簡單,不多做解釋。
光是這樣指定當然不夠,你會發現我這裡的 host 已經不是 github.com 了。
我為每個倉庫使用了自己的 host,每個 host 的網域名稱做 CNAME 解析到 github.com,這樣 ssh 在串連時就可以區別不同的倉庫了。
來測試下:
# ssh -T git@php-backend.github.quyun.net
Hi quyun/php-backend! You’ve successfully authenticated, but GitHub does not provide shell access.
# ssh -T git@aliyun-api-utils.github.quyun.net
Hi quyun/aliyun-api-tools! You’ve successfully authenticated, but GitHub does not provide shell access.
可以看到 ssh 已經能夠根據不同 host 使用不同私密金鑰了。
最後,將你的 git 倉庫地址中的 host 改為新設定的 host 即可,如上面的:
git@github.com:quyun/aliyun-api-tools.git
改為:
git@aliyun-api-tools.github.quyun.net:quyun/aliyun-api-tools.git
在 windows 下也會出現這個問題,用同樣的方式解決即可!