在 Windows 中使用 Git 的多个 Github 帐户

Sch*_*ter 5 git github git-push git-for-windows

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

基本上是这样的

  • 第一次使用 git,它在推送到存储库时要求提供 github 凭据。然后,无论 repo 是如何克隆的(哪个 ssh 密钥、用户等),这些凭据都将用于所有推送
  • 为两个 github 帐户生成 SSH 密钥,并将条目添加到 ssh 配置以定位这些身份文件。密钥也会添加到每个 github 帐户中。
  • 使用原始帐户 git clone :/.git 的 ssh 配置中的相应主机条目克隆 repo
  • 尝试将更改推送到 repo 并成功克隆 repo 使用 ssh 配置中的相应主机条目为第二个帐户 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
Run Code Online (Sandbox Code Playgroud)

我将这个帖子发布给遇到此问题的其他人,并提出一些问题,

  1. 我知道这个命令本质上是指定 ssh 连接在推送时使用的密钥,但是如果使用相同的身份文件克隆这个密钥,为什么还没有针对它?

  2. 是否有其他替代方法或更好的方法,而不是像手动更改配置值或从 Windows 凭据管理器中删除条目那样繁琐的工作?

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


HTTP 路径

https://github.com/schwaggs/testssh

https://github.com/jjschweigert/testrepo

SSH 路径

git@github.com:schwaggs/testssh.git

git@github.com:jjschweigert/testrepo.git

SSH 配置文件

$ 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
Run Code Online (Sandbox Code Playgroud)

使用原始帐户克隆

$ 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.
Run Code Online (Sandbox Code Playgroud)

推送到原始帐户 (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
Run Code Online (Sandbox Code Playgroud)

从第二个帐户克隆 (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.
Run Code Online (Sandbox Code Playgroud)

推送到二级账户

$ 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.
Run Code Online (Sandbox Code Playgroud)

SSH -T 输出

$ 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.
Run Code Online (Sandbox Code Playgroud)

Von*_*onC 5

清除 Windows 凭据管理器中的 git 条目并不能解决此问题。

如果您使用 ssh URL ( ),则不git@github.com:<user>/<repo>涉及凭证管理器:它仅为 URL 提供凭证。https://

首次使用 git,在推送到存储库时需要 github 凭据。

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

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

这是通过 ssh 配置文件完成的:请参阅实际示例:


来自编辑

Host user1
 HostName github.com
 User git
 IdentityFile ~/.ssh/iser1_key  <<====
Host user2
 HostName github.com
 User git
 IdentityFile ~/.ssh/user1_key  <<==== same key!? Meaning same user!
Run Code Online (Sandbox Code Playgroud)

如果 user2 条目的 SSH 配置引用了 user1 私钥,则不能期望以 user2 身份进行推送。