git public key for more repositories

后端 未结 1 601
轮回少年
轮回少年 2021-01-16 02:56

I\'m having following problem.

I have 2 projects and I use github. 1st project went smooth, I created a public key, entered passphrase and pushed into github. no pro

相关标签:
1条回答
  • 2021-01-16 03:30

    You need to declare your different ssh keys in a ~/.ssh/config file, as I explained in "How to manage multiple ssh keys in the ~/.ssh directory"

    I would recommend not using the default name for the keys, but rather:

    ~/.ssh/proj1
    ~/.ssh/proj1.pub
    ~/.ssh/proj2
    ~/.ssh/proj2.pub
    

    And then have a ~/.ssh/config like:

    Host ghproj1
        User           git
        Hostname       github.com
        IdentityFile   ~/.ssh/proj1
        IdentitiesOnly yes
    Host ghproj2
        User           git
        Hostname       github.com
        IdentityFile   ~/.ssh/proj2
        IdentitiesOnly yes
    

    You need to change the origin url in both repos:

    cd /path/to/cloned/proj1
    git remote set-url origin ghproj1:yourProject1
    
    cd /path/to/cloned/proj2
    git remote set-url origin ghproj1:yourProject2
    

    See more at:

    • "How to change git ssh user for a remote push temporarily?"
    • "Having Trouble Switching Github accounts on terminal"

    An url like ghproj1:yourProject1 is an ssh one which will explicitly use the key you specified in ~/.ssh/config for the ghproj1 entry.

    In the OP's case (answer below), the correct url would be:

    ~/.ssh/id_recaprojekt
    

    Note: you need to specify the path to the private key (private, not public, not .pub)

    cd /path/to/cloned/plastickychirurg
    git remote set-url origin plastickychirurg:michalfeher/plastickychirurg.git
    
    cd /path/to/cloned/recaprojekt
    git remote set-url origin recaprojekt:michalfeher/recaprojekt.git
    

    Note that I have added "Hostname" in the Host entries.

    The all idea of those entries in the ~/.ssh/config file is to not (repeat not) put git or github.com in the url (it is done for you by the items associated to each entries):

    So:

     git@github.com:michalfeher/recaprojekt.git
    

    is the same as:

     recaprojekt:michalfeher/recaprojekt.git
    

    Except the second url will use the ssh key

    0 讨论(0)
提交回复
热议问题