Github for work and play (multiple accounts)

If you’re not using github, you should start (see my intro for tips on getting going).

One of the things that is likely to happen once you do get started is that you will want more than one account. This is may be because you need a work account and a personal account — or perhaps you have projects that need to be kept separate. Either way, multiple accounts are the thing.

This post is aimed at helping others to set up multiple github accounts (and to serve as a record for when I need to do it again). To get started, you will need to create a repo (see my tutorial if you are stuck at this step).

Screen Shot 2013-09-22 at 5.10.43 PM

Log into your repo, hit the settings button (marked in the above picture), and head into the SSH Key section (marked in the below picture).

Screen Shot 2013-09-22 at 5.15.26 PM

What we need to do here is to generate a new SSH key, and add it to the github account. So open your terminal app, and type in:

ssh-keygen -t rsa -C "your-email-here"

If you’ve done it right, you will see something like the following.

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/ra/.ssh/id_rsa):

At this step, you need to type in the file name attached to the key-pair. You may or may not already have an “id_rsa” from the setup phase of your github account — so you almost certainly want to save it as something different … say:

/Users/ra/.ssh/id_abc

You will be asked for a passphrase. Add one if you like.

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

Once you’ve done this, you will see something like the following:

Your identification has been saved in /Users/ra/.ssh/id_abc.
Your public key has been saved in /Users/ra/.ssh/id_abc.pub.
The key fingerprint is:
1a:b0:c2:d3:4e:f5:g7:hi:89:j0:12:34:5k:lm:n6:78 abc@hotmail.com
The key's randomart image is:
+--[ RSA 1234]----+
...
+-----------------+

You need to tell your computer about the new key-pair. To do this, type ssh-add ~/.ssh/id_abc into the terminal (substituting your key name in place of my id_abc). It’s a good idea to re-start your terminal application at this point — just to be sure.

Note, if the key was given a passphrase, you’ll need to add that to the keychain, else you will have ssh-add the key after each reset (see this stackoverflow question for more detail).

To do this, use: ssh-add -K /path/to/private/key/file

If you want to check what keys have been added, use: ssh-add -l

Next, move to your .ssh folder and you’ll see two new files: these are the public and private parts of the key. The public side ends with .pub — you want to open this up in a text editor (i use vim, but you can use whatever you like) and copy the key to the clipboard.

$ cd ~/.ssh
$ ls -la
$ vim id_abc.pub

If you are comfortable at the command line, forget opening the file and pipe directly to the pasteboard using:

$ cat id_abc.pub | pbcopy

Screen Shot 2013-09-22 at 5.33.56 PM

Next, we need to add this ‘key’ to your github account. To do this, hit the “Add SSH Key” button (see above), and then paste in the public part of the key.

You can give it whatever name you like: I suggest something that will help you remember what the key does.

Screen Shot 2013-09-22 at 5.34.19 PM

If things are going to plan, it should look something like the above. Hit the add key button.

Almost there … now for the tricky bit. We need to tell your computer which key to use and when. To do this we really do need a text editor (command line tricks won’t cut it this time).

To do this, we need to create a config file and add some instructions.


$ touch ~/.ssh/config
$ vim ~/.ssh/config

Now cut and paste in the following:


Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

Host github_abc
HostName github.com
User git
IdentityFile ~/.ssh/id_abc

The first chunk tells your computer to use the original key-pair (say your personal one, which was already set up — if you do not have an id_rsa key-pair, create one and add it using the above steps); the second chunk tells it to use the new key-pair (perhaps the new ‘work’ key-pair).

Note, the hostname remains github.com and the user remains git.

Finally, to see if everything is working, give it all a test-drive. Note that the name of the origin in the below command is NOT git@github.com.


$ mkdir abc
$ cd abc
$ touch README.md
$ echo "hello world" >> README.md
$ git init
$ git add README.md
$ git commit -m "first commit"
$ git remote add origin git@github_abc:accountName/abc.git
$ git push -u origin master

In an ordinary case, the last line would have been

$ git remote add origin git@github.com:accountName/abc.git

However we change that bit so that your computer knows which key to use — again it’s different … you need to get this bit right!

$ git remote add origin git@github_abc:accountName/abc.git

Note, if you do not configure a local username and email, it will use the global defaults (which may or may not be appropriate for your commits).

If you wish to have your commits marked differently (with a different user name and email for each project), you must do the following:

$ git config user.name "abcMan"
$ git config user.email "abc@hotmail.com"

If you are unsure what’s been set, and want to check, simply supply the above without final argument (so $ git config user.name); the user.name and user.email that are currently set will be returned.

Finally, note that other commands, such as cloning, can also be configured on a per account basis.

So rather than the usual:

$ git clone git@github.com:other/otherproject.git

If you wished to clone it as ‘abc’, you would supply:

$ git clone git@github_abc:other/otherproject.git

Advertisements
This entry was posted in coding, tech and tagged . Bookmark the permalink.

please comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s