【问题标题】:Maintaining two github user accounts维护两个 github 用户帐户
【发布时间】:2016-12-08 07:17:06
【问题描述】:

我从两个不同的帐户使用 github,一个是专业的,一个是个人的。当 git 将一些代码推送到我的个人帐户上的存储库时,它正在尝试使用专业帐户的用户名进行推送

git push origin master 说:
Permission to X/abc.git denied to Y fatal: unable to access 'https://github.com/X/abc.git/': The requested URL returned error: 403

但是 git remote -v 说:
来源https://github.com/X/abc.git(获取)
来源https://github.com/X/abc.git(推)

我已经检查过了
1- 我的 ssh 密钥存在于我的个人帐户中
2- ssh@github.com -v 的 O/p:

OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: Connecting to github.com [192.30.253.112] port 22.
debug1: Connection established.
debug1: identity file /Users/akash.bansal/.ssh/id_rsa type 1
debug1: identity file /Users/akash.bansal/.ssh/id_rsa-cert type -1
debug1: identity file /Users/akash.bansal/.ssh/id_dsa type -1
debug1: identity file /Users/akash.bansal/.ssh/id_dsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.2
debug1: Remote protocol version 2.0, remote software version libssh-0.7.0
debug1: no match: libssh-0.7.0
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-sha1 none
debug1: kex: client->server aes128-ctr hmac-sha1 none
debug1: sending SSH2_MSG_KEXDH_INIT
debug1: expecting SSH2_MSG_KEXDH_REPLY
debug1: Server host key: RSA     16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /Users/akash.bansal/.ssh/known_hosts:77
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/akash.bansal/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
Authenticated to github.com ([192.30.253.112]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LC_CTYPE = UTF-8
PTY allocation request failed on channel 0
Hi X! You've successfully authenticated, but GitHub does not provide shell access.

3- git config 用户名
X
4- git config user.email
X's email
5- git config 读取

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/X/abc.git
fetch = +refs/heads/*:refs/remotes/origin/*

附:此 repo 目录是在本地创建的,然后尝试与远程目录链接。如果我克隆一个远程目录,一切正常。 请告诉我我错过了什么?

【问题讨论】:

  • 您似乎对git和github的区别感到很困惑。你没有 git 账户;您在 github 上有一个帐户。
  • 虽然我确信你可以让它工作,而且我自己做,但 github 建议个人和专业都使用 single 帐户。有道理。

标签: git github


【解决方案1】:

配置user.nameuser.email 仅在提交时相关。这就是 Git 将作为提交的作者和提交者写入提交对象的内容。但是,它不会影响远程存储库的任何身份验证; Git其实没有认证的概念,只有连接的传输层提供。

在您的情况下,您使用的是 SSH。 SSH 将使用公钥/私钥组合对您进行身份验证。默认情况下,SSH 将使用位于 ~/.ssh/id_rsa 的密钥,这也是您的日志确认的内容:

debug1: Offering RSA public key: /Users/akash.bansal/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279

因此,该密钥被用于对您进行身份验证。它可能属于您的专业帐户,因此 GitHub 只会看到您的专业帐户,而不会让您访问您的个人存储库。

为了改变这一点,您必须提供不同的 SSH 密钥。这有点复杂,因为两个密钥都针对同一个主机(GitHub)。不过,您可以为此设置 SSH 配置。为此,请创建 ~/.ssh/config 并将以下内容放入其中(修改它以匹配您的公钥路径):

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/personal-profile_id_rsa

完成后,您可以使用远程 URL git@github-personal:X/abc.git 通过您的个人 密钥访问存储库X/abc.git,并使用git@github.com:X/abc.git 通过您的专业人员访问它em> 键。

或者,您也可以切换到 HTTPS URL,因为它们允许您在 URL 中指定您的用户名:

https://professional@github.com/X/abc.git
https://personal@github.com/X/abc.git

【讨论】:

  • 宾果游戏!它有效,我认为需要在私人 github 帐户中添加私人 ssh 密钥
  • 是的,当然您需要将您的专业帐户和个人帐户的 public 密钥添加到各自的 GitHub 帐户。
猜你喜欢
  • 1970-01-01
  • 2019-07-10
  • 2018-03-11
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
  • 2021-04-29
  • 2018-08-22
  • 2012-12-29
相关资源
最近更新 更多