How do I push to GitHub under a different username?

后端 未结 20 1921
盖世英雄少女心
盖世英雄少女心 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:48

    If you use an SSH URL (git@github.com:username/projectname.git) rather than an HTTPS URL, you can temporarily specify a different SSH key using the $GIT_SSH_COMMAND environment variable:

    $ GIT_SSH_COMMAND="ssh -i different_private_key" git push
    

    As far as I can tell, with SSH URLs, GitHub doesn't care about user names, only about keys: if a user account has access to a repository, and that account has an SSH key (see the SSH keys page in the account settings), then if you use that SSH key to push to that repository, the push will be treated as coming from that user.

    0 讨论(0)
  • 2020-12-02 04:50

    I couldn't figure out how to have a 2nd github identity on the one machine (none of these answers worked for me for that), but I did figure out how to be able to push to multiple different github accounts as myself.

    Push as same username, but to different github accounts

    1. Set up a 2nd SSH key (like so) for your 2nd github account

    2. Change between accounts thus :

    Push with my new 2nd github account

    ssh-add -D
    ssh-add ~/.ssh/ssh_key_for_my_2nd_account
    git push
    

    Push with my main account

    ssh-add -D
    ssh-add ~/.ssh/id_rsa   
    git push
    
    0 讨论(0)
提交回复
热议问题