Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

git - Multiple GitHub accounts on the same computer?

Trying to work on both my actual "work" repos, and my repos on git hub, from my computer.

The work account was set up first, and everything works flawlessly.

My account, however, cannot seem to push to my repo, which is set up under a different account/email.

I've tried copying my work key up to my account, but that throws an error because of course a key can be only attached to one account.

How can I push/pull to and from both accounts from their respective GitHub credentials?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

All you need to do is configure your SSH setup with multiple SSH keypairs.

Relevant steps from the first link:

  1. Generate an SSH-key ssh-keygen -t rsa -C "[email protected]", follow the prompts and decide a name, e.g. id_rsa_doe_company.
  2. Copy the SSH public-key to GitHub from ~/.ssh/id_rsa_doe_company.pub and tell ssh about the key: ssh-add ~/.ssh/id_rsa_doe_company.
  3. Create a config file in ~/.ssh with the following contents:
    Host github-doe-company
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_doe_company
    
  4. Add your remote git remote add origin git@github-doe-company:username/repo.git or change using git remote set-url origin git@github-doe-company:username/repo.git

Also, if you're working with multiple repositories using different personas, you need to make sure that your individual repositories have the user settings overridden accordingly:

Setting user name, email and GitHub token – Overriding settings for individual repos https://help.github.com/articles/setting-your-commit-email-address-in-git/

Hope this helps.

Note: Some of you may require different emails to be used for different repositories, from git 2.13 you can set the email on a directory basis by editing the global config file found at: ~/.gitconfig using conditionals like so:

[user]
    name = Pavan Kataria
    email = [email protected]

[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig

And then your work specific config ~/work/.gitconfig would look like this:

[user]
    email = [email protected]

Thank you @alexg for informing me of this in the comments.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...