taoCMS是基于php+sqlite/mysql的国内最小(100Kb左右)的功能完善、开源免费的CMS管理系统

How to Work with GitHub and Multiple Accounts

2013-10-28

So you have a personal GitHub account; everything is working perfectly. But then, you get a new job, and now need to have the ability to push and pull to multiple accounts. How do you do that? I’ll show you how!


Prefer a Screencast?

Choose 720p for the best picture.

Step 1 – Create a New SSH Key

We need to generate a unique SSH key for our second GitHub account.

1
ssh-keygen -t rsa -C "your-email-address"

Be careful that you don’t over-write your existing key for your personal account. Instead, when prompted, save the file as id_rsa_COMPANY. In my case, I’ve saved the file to ~/.ssh/id_rsa_nettuts.


Step 2 – Attach the New Key

Next, login to your second GitHub account, browse to “Account Overview,” and attach the new key, within the “SSH Public Keys” section. To retrieve the value of the key that you just created, return to the Terminal, and type: vim ~/.ssh/id_rsa_COMPANY.pub. Copy the entire string that is displayed, and paste this into the GitHub textarea. Feel free to give it any title you wish.

Next, because we saved our key with a unique name, we need to tell SSH about it. Within the Terminal, type: ssh-add ~/.ssh/id_rsa_COMPANY. If successful, you’ll see a response of “Identity Added.”


Step 3 – Create a Config File

We’ve done the bulk of the workload; but now we need a way to specify when we wish to push to our personal account, and when we should instead push to our company account. To do so, let’s create aconfig file.

1
2
touch ~/.ssh/config
vim config

If you’re not comfortable with Vim, feel free to open it within any editor of your choice. Paste in the following snippet.

1
2
3
4
5
#Default GitHub
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

This is the default setup for pushing to our personal GitHub account. Notice that we’re able to attach an identity file to the host. Let’s add another one for the company account. Directly below the code above, add:

1
2
3
4
Host github-COMPANY
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_COMPANY

This time, rather than setting the host to github.com, we’ve named it as github-COMPANY. The difference is that we’re now attaching the new identity file that we created previously: id_rsa_COMPANY. Save the page and exit!


Step 4 – Try it Out

It’s time to see if our efforts were successful. Create a test directory, initialize git, and create your first commit.

1
2
git init
git commit -am "first commit'

Login to your company account, create a new repository, give it a name of “Test,” and then return to the Terminal and push your git repo to GitHub.

1
2
git remote add origin git@github-COMPANY:Company/testing.git
git push origin master

Note that, this time, rather than pushing to git@github.com, we’re using the custom host that we create in the
config file: git@github-COMPANY.

Return to GitHub, and you should now see your repository. Remember:

  • When pushing to your personal account, proceed as you always have.
  • For your company account, make sure that you use git!github-COMPANY as the host.

Be sure to refer to the screencast if you need a more visual overview of the steps above!

类别:技术文章 | 阅读:282249 | 评论:1 | 标签:

想收藏或者和大家分享这篇好文章→

“How to Work with GitHub and Multiple Accounts”共有1条留言

  1. persmile @2013-10-31 22:15 回复

    you should set the global(app)'s committer name and email.
    eg:
    git config --global user.name
    git config --global user.email

发表评论

姓名:

邮箱:

网址:

验证码:

公告

taoCMS发布taoCMS 3.0.2(最后更新21年03月15日),请大家速速升级,欢迎大家试用和提出您宝贵的意见建议。

捐助与联系

☟请使用新浪微博联系我☟

☟在github上follow我☟

标签云