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

标签 git github git-push git-for-windows

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

基本上是这样的,

  • 第一次使用 git 时需要 github 凭据 推送到存储库。然后将这些凭据用于所有 无论 repo 是如何克隆的(哪个 ssh key 、用户、 等等)
  • 为两个 github 帐户生成 SSH key 并将条目添加到 ssh 配置以这些身份文件为目标。键被添加到每个 github 帐户也是如此。
  • 使用原始 ssh 配置中相应的主机条目克隆 repo 账号 git clone :/.git
  • 尝试将更改推送到存储库并成功克隆存储库使用 第二个帐户 git clone 的 ssh 配置中相应的主机条目 <2nd Host>:<2nd username>/.git
  • 尝试将更改推送到 repo 并收到原始错误 用户名没有权限,即使这是使用克隆的 第二个用户,更具体地说是一个 ssh key 。

  • 清除 Windows 凭据管理器中的 git 条目没有 解决这个问题。

  • 清除全局用户名和电子邮件未解决此问题

我终于能够使用以下方法推送我的更改:

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

我将此发布给遇到此问题的其他人,同时也想问几个问题,

  1. 我理解此命令实质上是指定 ssh 连接在推送时要使用的 key ,但如果使用相同的身份文件克隆它,为什么它尚未成为目标?

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

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


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

使用原始帐户克隆

$ 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.

推送到原始帐户 (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

从第二个帐户 (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.

推送到二级账户

$ 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 输出

$ 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.

最佳答案

Clearing the git entries in the windows credential manager did not resolve this issue.

如果您使用的是 ssh URL ( git@github.com:<user>/<repo> ),凭证管理器参与:它为 https:// 提供凭证仅限 URL。

Use git for the first time which asks for github credentials when pushing to a repository.

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

So the goal would be to push changes to multiple github accounts without having to do things like temporarily specify the ssh key to use.

这是通过 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!

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

关于git - 在 Windows 中使用 Git 的多个 Github 帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55273735/

相关文章:

eclipse - 在没有 .project 文件的情况下将现有 Git 项目导入 Eclipse

git - 不 merge pull 远程分支

git - 查找错误来源 : git push rejected: error: failed to push some refs

git - 无法推送到 Windows 上的 git 裸存储库

git - 致命 : Not a gitflow-enabled repo yet. 请先运行 'git flow init'

git - 将 Github 的更改 pull 到 Github

Git 从不(自动) merge composer.json

github - 如何在启用强制 HTTPS 的情况下修复自定义域设置的 Github 页面中的 : Domain does not resolve to the GitHub Pages server. 错误?

git - 在 github 上的开源项目的 Pull request 中,维护者应该如何信任原作者?

github - 将 GitHub 存储库私有(private)化会使明星消失?