维护两个github用户帐户

时间:2016-08-02 19:21:03

标签: git github

我使用来自两个不同账户的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 O / p ssh@github.com -v:

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 user.name
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/*

P.S。此repo目录在本地创建,然后尝试链接到远程目录。如果我克隆远程目录,一切正常。 请告诉我我错过了什么?

1 个答案:

答案 0 :(得分:3)

配置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

完成后,您可以使用远程网址git@github-personal:X/abc.git使用个人键访问存储库X/abc.git,并使用git@github.com:X/abc.git访问存储库https://professional@github.com/X/abc.git https://personal@github.com/X/abc.git 专业键。

或者,您也可以切换到HTTPS网址,因为这些网址允许您在网址中指定用户名:

Array
(
    [0] => Array
        (
            [H2H_Id] => T32
            [Team1_Id] => T4
            [Team1] => Juan Arraya - Max LePivert
            [Team2_Id] => T205
            [Team2] => Marco Grangeiro - Jeff Morneau
            [Winners_Id] => T4
            [MatchUps_Id] => M32
        )

    [1] => Array
        (
            [H2H_Id] => T39
            [Team1_Id] => T205
            [Team1] => Marco Grangeiro - Jeff Morneau
            [Team2_Id] => T4
            [Team2] => Juan Arraya - Max LePivert
            [Winners_Id] => T205
            [MatchUps_Id] => M32
        )

    [2] => Array
        (
            [H2H_Id] => T9
            [Team1_Id] => T3
            [Team1] => Marco Grangeiro - George Wilkinson
            [Team2_Id] => T4
            [Team2] => Juan Arraya - Max LePivert
            [Winners_Id] => T4
            [MatchUps_Id] => M9
        )

)