How do I push to GitHub under a different username?

后端 未结 20 2033
盖世英雄少女心
盖世英雄少女心 2020-12-02 04:00

A friend and myself are sharing my computer. I\'ve made pushes to GitHub using the git bash shell on Windows 7. Now we\'re in a different project on that computer and I need

20条回答
  •  盖世英雄少女心
    2020-12-02 04:28

    I have been using one machine to push code to two different GitHub accounts with different username. Assuming you already set up one account and want to add a new one:

    1. Generate new SSH key ssh-keygen -t rsa -C "myaddress@example.com"
    2. Save it, but remember not to override the existent one id_rsa. Give it a different name, e.g. id_rsa_another
    3. Copy the contents of the key to your GitHub account:

    Settings -> SSH and GPG keys -> New SSH key -> Give a label and paste the key -> Add SSH key

    1. Add the key to the ssh agent: ssh-add ~/.ssh/id_rsa_another
    2. Setup a GitHub host: Create a config file with touch ~/.ssh/config and edit the file by providing configurations to your accounts:
    #first account
    Host github.com-first
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa
    
    #another account
    Host github.com-another
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa_another
    

    Now you should be able to push from different accounts depending on what key you add to the ssh agent, i.e. to use your first account, do ssh-add ~/.ssh/id_rsa.

    You might also want to change your user email:

    git config --global user.email "myaddress@example.com"

    or clean out ssh keys in case of permission error when pusing code to one of the accounts:

    ssh-add -D

提交回复
热议问题