Windows中具有Git的多个Github帐户 [英] Multiple Github Accounts With Git In Windows

查看:120
本文介绍了Windows中具有Git的多个Github帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I recently ran into an issue where I could not push changes into a repository I had cloned down as another user from the first user I pushed with in git on my desktop.

基本上是这样的,

  • 第一次使用git时要求github凭证 推送到存储库.然后将这些凭据用于所有 无论回购是如何克隆的(无论哪个ssh密钥,用户, 等)
  • 为两个github帐户生成SSH密钥,并将条目添加到 ssh config将这些标识文件作为目标.密钥被添加到每个 还有github帐户.
  • 使用ssh config中的相应主机条目克隆原始存储库 帐户git clone:/.git
  • 尝试将更改推送到存储库,并成功使用克隆存储库 ssh配置中第二个帐户git clone的相应主机条目 < 2nd Host>:< 2nd用户名>/.git
  • 尝试将更改推送到存储库并收到原始错误 用户名没有权限,即使已使用克隆了该用户名 第二个用户,更具体地说是ssh密钥.

  • Use git for the first time which asks for github credentials when pushing to a repository. These credentials are then used for all pushes regardless of how the repo was cloned (which ssh key, user, etc)
  • Generate SSH keys for both github accounts and add entries to the ssh config to target these identity files. Keys are added to each github account as well.
  • Clone repo using corresponding Host entry in ssh config for original account git clone :/.git
  • Attempt to push changes to repo and is successful Clone repo using corresponding Host entry in ssh config for second account git clone <2nd Host>:<2nd username>/.git
  • Attempt to push changes to repo and receive error that the original username does not have permission, even though this was cloned using the second user and more specifically an ssh key.

在Windows凭据管理器中清除git条目没有 解决此问题.

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

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

Clearing the global user name and email did not resolve this issue

我终于可以使用以下命令推送更改:

I was finally able to push my changes using the following:

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

我既要为遇到此问题的其他人发布此信息,又要提出一些问题,

I am posting this both for others who have experienced this issue and also to ask a few questions,

  1. 我知道该命令实际上是在为ssh连接进行推送时指定要使用的密钥,但是如果使用相同的身份文件克隆了该密钥,为什么还没有将其锁定为目标?

  1. I understand this command is essentially specifying the key for the ssh connection to use when it makes it's push, but why is this key not already targeted if it was cloned using that same identity file?

是否有其他替代方法或更好的方法,这些替代方法不是繁琐的工作,例如手动更改配置值或从Windows凭据管理器中删除条目?

Are there any alternatives to this or better approaches that are not tedious work like manually changing config values or removing entries from the windows credential manager?

因此,目标是将更改推送到多个github帐户,而不必执行诸如临时指定要使用的ssh密钥之类的事情.

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.

https://github.com/schwaggs/testssh

https://github.com/jjschweigert/testrepo

git@github.com:schwaggs/testssh.git

git@github.com:schwaggs/testssh.git

git@github.com:jjschweigert/testrepo.git

git@github.com:jjschweigert/testrepo.git

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

推荐答案

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

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

如果您使用的是ssh URL(git@github.com:<user>/<repo>),则不使用凭据管理器: :它仅提供https:// URLS的凭据.

If you are using ssh URL (git@github.com:<user>/<repo>), the credential manager is not involved: it provides credentials for https:// URLS only.

首次使用git时,它会在推送到存储库时要求github凭据.

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

只有当远程来源URL是一个https时,情况才会如此.

That would be the case only if the remote origin URL is an https one.

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

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配置文件完成的:请参见实际示例:

That is done through an ssh config file: see as practical examples:

  • "How to work on personal GitHub repo from office computer whose SSH key is already added to a work related GitHub account?"
  • "How to set up authentication for two separate GitHub accounts from same ssh client?"

来自修改

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的身份进行推送.

you cannot expect push as user2, if the SSH config for user2 entry refers to user1 private key.

这篇关于Windows中具有Git的多个Github帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆