我有两个 github 帐户,我正在尝试配置 github、我的环境和 ssh key ,以便我能够推送到任何存储库,无论它位于我的 github.com/user1 还是 github.com/user2 帐户中。我花了几个小时试图按照我能找到的所有资源来解决这个问题,可惜我似乎缺乏解决这个问题的理解。谁能帮我正确配置?
我在 github.com 上的两个帐户都设置了不同的电子邮件地址; github.com/user1 = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1762647265265762647265263974787a" rel="noreferrer noopener nofollow">[email protected]</a>
和github.com/user2 = <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e297918790d0a297918790d0cc818d8f" rel="noreferrer noopener nofollow">[email protected]</a>
.
我有两个生成的 SSH key ,第一个位于 .ssh/id_rsa
& .ssh/id_rsa.pub
这个 SSH key 保存在 github.com/user1
中。 .ssh/user2
中的第二个 SSH key .ssh/user2_rsa and saved in
github.com/user1`。
我有一个.ssh/config file
其中包含:
Host user1.github.com
HostName github.com
IdentityFile ~/.ssh/user1
Host user2.github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa
当我运行ssh-add -l
时我看到两个键都已“加载”。
当我运行ssh -T <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aec9c7daeedbddcbdc9f80c9c7dac6dbcc80cdc1c3" rel="noreferrer noopener nofollow">[email protected]</a>
时我得到Hi user2! You've successfully authenticated, but GitHub does not provide shell access.
当我运行ssh -T <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="65020c112510160017574b020c110d10074b060a08" rel="noreferrer noopener nofollow">[email protected]</a>
时我得到Hi user2! You've successfully authenticated, but GitHub does not provide shell access.
我可以成功推送到“github.com/user2”中的存储库。
我无法推送到“github.com/user1”中的存储库,并显示以下错误消息;
ERROR: Permission to user1/repo.git denied to user2.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
我的“.gitconfig”文件包含:
[user]
name = my full name
email = <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0174726473304174726473302f626e6c" rel="noreferrer noopener nofollow">[email protected]</a>
[GitHub]
user = user1
据我所知,我的本地环境 cmd 使我被困在 user2 中,并且不允许 user2 提交到 user1 github 帐户。但是a)我认为.ssh/config
中的脚本应该认识到,当我推送到 user1 时,它应该使用 user1 ssh,反之亦然,但这似乎没有发生,b) 我的 .gitcofig
有我的user.email
我的 GitHub 用户为 user1,这与运行 ssh -T ...
后我被验证为 user2 相冲突.
我已经尝试了很多东西,我的内裤现在在这一条上有了适当的转变。任何人都可以帮我正确设置这个,或者指导我如何删除所有 SSH 并重新开始并正确设置(请注意,我还设置了一个需要配置的 Heroku 帐户)。
提前致谢。
最佳答案
The 2nd SSH key in
.ssh/user2 .ssh/user2_rsa
然而,你的.ssh/config
使用id_rsa
对于 user2.github.com
Host user2.github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa
^^^^^^
和user1.pub
公钥似乎已在 user2
上注册帐户,自 ssh -T <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d3a34291d282e382f6c733a342935283f733e3230" rel="noreferrer noopener nofollow">[email protected]</a>
返回 Hi user2!
检查您的本地配置,并在 github 端检查哪个公钥已添加到哪个帐户。
注意:添加用户git
在 .ssh/config
推荐文件:
Host user2.github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa
User git <=====
这样,URL 就变成了:
ssh -T user2.github.com
关于Git 错误 : Permission to user1/repoA. git 拒绝 user2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32958482/