Windows中使用Git的多个Github帐户

问题描述 投票:2回答:2

我最近遇到了一个问题,我无法将更改推送到我已经克隆下来的存储库作为我在桌面上使用git推送的第一个用户中的另一个用户。

基本上它是这样的,

  • 首次使用git,在推送到存储库时会询问github凭据。无论克隆如何克隆(哪个ssh密钥,用户等),这些凭据都将用于所有推送
  • 为两个github帐户生成SSH密钥,并向ssh配置添加条目以定位这些标识文件。密钥也添加到每个github帐户。
  • 使用ssh config中相应的Host条目克隆原始帐户git clone:/。git
  • 尝试将更改推送到repo并成功使用ssh config中的相应主机条目克隆第二个帐户git clone <2nd Host>:<2nd username> / .git
  • 尝试将更改推送到repo并收到原始用户名没有权限的错误,即使这是使用第二个用户克隆的,更具体地说是使用ssh密钥克隆的。
  • 清除Windows凭据管理器中的git条目无法解决此问题。
  • 清除全局用户名和电子邮件无法解决此问题

我终于能够使用以下内容推送我的更改:

GIT_SSH_COMMAND="ssh -i <path to private ssh key for second user>" git push

我正在为经历过这个问题的其他人发帖,并提出几个问题,

  1. 我理解这个命令实际上是指定ssh连接在推送时使用的密钥,但是如果使用相同的身份文件克隆该密钥,为什么该密钥尚未被定位?
  2. 有没有替代这种或更好的方法,如手动更改配置值或从Windows凭证管理器中删除条目不繁琐的工作?

因此,目标是将更改推送到多个github帐户,而无需执行临时指定要使用的ssh密钥之类的操作。


HTTP Paths

https://github.com/schwaggs/testssh

https://github.com/jjschweigert/testrepo

SSH Paths

[email protected]:schwaggs / testssh.git

[email protected]:jjschweigert / testrepo.git

SSH Config File

$ cat ~/.ssh/config
Host jjschweigert
 HostName github.com
 User git
 IdentityFile ~/.ssh/jjschweigert_key
Host schwaggs
 HostName github.com
 User git
 IdentityFile ~/.ssh/jjschweigert_key

Cloning With Original Account

$ git clone jjschweigert:jjschweigert/testrepo.git
Cloning into 'testrepo'...
remote: Enumerating objects: 28, done.
remote: Counting objects: 100% (28/28), done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 28 (delta 0), reused 28 (delta 0), pack-reused 0
Receiving objects: 100% (28/28), done.

Pushing To Original Account (jjschweigert)

$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 261 bytes | 43.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To jjschweigert:jjschweigert/testrepo.git
   c082e38..31b7830  master -> master

Cloning From Second Account (schwaggs)

$ git clone schwaggs:schwaggs/testssh.git
Cloning into 'testssh'...
remote: Enumerating objects: 21, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 21 (delta 0), reused 18 (delta 0), pack-reused 0
Receiving objects: 100% (21/21), done.

Pushing To Secondary Account

$ git push
ERROR: Permission to schwaggs/testssh.git denied to jjschweigert.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

SSH -T Outputs

$ ssh -T jjschweigert
Hi jjschweigert! You've successfully authenticated, but GitHub does not provide shell access.


$ ssh -T schwaggs
Hi jjschweigert! You've successfully authenticated, but GitHub does not provide shell access.
git github git-push git-for-windows
2个回答
3
投票

清除Windows凭据管理器中的git条目无法解决此问题。

如果您使用的是ssh URL([email protected]:<user>/<repo>),则不涉及凭证管理器:它仅为https:// URL提供凭据。

首次使用git,在推送到存储库时会询问github凭据。

仅当远程源URL是https URL时才会出现这种情况。

因此,目标是将更改推送到多个github帐户,而无需执行临时指定要使用的ssh密钥之类的操作。

这是通过ssh配置文件完成的:参见实例:

  • “Qazxswpoi”
  • “Qazxswpoi”

从编辑

How to work on personal GitHub repo from office computer whose SSH key is already added to a work related GitHub account?

如果user2条目的SSH配置引用user1私钥,则不能指望push为user2。


1
投票

根据与VonC的对话,您可以在ssh配置文件中清楚地看到第二个用户的身份文件不正确,因为它指向第一个用户文件。第二个条目从第一个条目复制而且此值未更改。

修改值以指向正确的键,即〜/ .ssh / schwaggs_key后,我可以克隆并推送没有问题。作为旁注,我必须在git中为每个用户提取的每个存储库设置用户的电子邮件和名称属性,即一次在回购中,

How to set up authentication for two separate GitHub accounts from same ssh client?
© www.soinside.com 2019 - 2024. All rights reserved.