Multiple GitHub accounts on the same machine
By: Dmytro Shamenko
Tags: OS X, Github, Git, Automation, Scripting

This article is not copyright of the tons of previous articles with a similar name, this is more as a personal run-book for me to memorize this process.

The Problem

When you need to work on several projects at the same time, you may be asked by your security managers to create an additional Github account which should be different from your account.

If you ask my opinion regarding this security move, I take it negatively, but can’t resist.

But imagine the situation when you need three, five different accounts and you need to work with them simultaneously, pulling and pushing commits several times per hour. 🧑🏼‍💻😭

Solution

God bless the internet, even with a variety of shit content, we are still able to find a solution and tweak it for personal needs.

With three components, we will be able to use several accounts at the same time without pain.

.ssh/config .gitconfig small Bash script

Please note, this process is described for Mac OS X and Linux users. I’m sorry if you are using Windows and won’t be able to reduce your pain using this run-book. I will try to update this article ASAP after validation this process with several Windows friends.

SSH

Yes, I work with GitHub using ssh keys and this is one more step to ease the process.

If you are familiar with got, to use the SSH method, you need to create ssh-key using ssh-keygen and update you ssh-agent. This method is described in this article.

Note: you can generate the key using the default **id_rsa** path. But this might be a problem if you need more than one account. GitHub doesn’t allow you to reuse the same key for more than one account.

Folders

Ideally, your workspace should be separated from your contribution. If you are cloning all repositories in a single folder, you might need to think to find a time for making everything tidy.

Example

- Documents/

— Projects/

——— Google/

——— Microsoft/

——— Personal/

The example above is fictional. I won’t be able to work at Google and Microsoft at the same time. 😭 I hope one time it will happen with me, as TVC for example.

But we will imagine this picture and they both will require me to create accounts in the manner firstname-last name-company using corporate emails firstname.lastname@company.com.

Let’s walk through the process starting with the keys.

Generate keys with custom filenames

In this article, you can find a flag -f which allows you to generate a key with a custom file name instead of entering it interactively.

$ ssh-keygen -f ~/.ssh/id_rsa_google -C dmytro.shamenko@google.com

$ ssh-keygen -f ~/.ssh/id_rsa_microsoft -C dmytro.shamenko@microsoft.com

The commands above will create for you two different keys, which can be used for different accounts at GitHub.

ssh-config

To define when the system should use which key, we need to configure it.

At ~/.ssh/ folder you might have config file which we need to update.

For each key (account/project), we need to create a configuration block.

Host github-microsoft

HostName github.com

User git

IdentityFile ~/.ssh/id_rsa_microsoft

And the same block of lines needs to be created for the Google key.

When you will finish these steps, you can proceed with the .gitconfig part.

.gitconfig

Git configuration is the way how the system will mark your changes. Who will own it? For using git you need to set two configurations first, user.name and user.email. These two parameters will be assigned with your commits.

The user name can be set as — global configuration, but email can’t. You need to use company based email for different accounts.

General configuration for git is located at $HOME folder, s you need to open or create file at ~/.gitconfig and edit general section [user]

[user]

name = Dmytro Shamenko

email = dmytroshamenko@gmail.com

This section will correspond for default settings. To achieve additional custom config, you need to add two include sections.

[includeIf “gitdir:~/Documents/Projects/Microsoft”]

path = ~/Documents/Projects/Microsoft /.gitconfig

This part will extend our configuration based on condition, when we work for Microsoft, but we need to create this file. Body should contain two lines.

[user]

email = dmytro.shamenko@microsoft.com

Action

Now we are ready to clone Microsoft repository with right key, but the clone URL should be different. Domain github.com need to be changed to github-microsoft, based on this change our ssh-config will do a magic.

Let be honest, this additional work is not good, when we need to clone new repos few times per a day. We areengineers, we are lazy enough, lets automate this part as well.

Shell script

To reduce manual work when we need to clone the repo, we can wrap git clone using bash scripting.

#!/bin/bash if [ $# -eq 0 ]

then

echo "No arguments supplied"

elif [ -z "$1" ]

then

echo "First argument should be project name (eg: microsoft, google)"

elif [ -z "$2" ]

then

echo "Second argument should be github.com url for clone"

else

url=`echo $2 | sed "s/\github.com/github-$1/g"`

$(eval "git clone $url")

fi

Put this script to ~/bin with the name ghclone.sh .

Note: you need to add $HOME/bin path to $PATH or you canuse symlink to /usr/local/bin with name ghclone.

$ ln -sf ~/bin/ghclone.sh /usr/local/bin/ghclone

This script will reduce manual work to complete zero. Now your clone process will looks like:

$ ghclone microsoft git@github.com/microsoft/microservice-repo.git

As a result, you will get cloned repository with remote configured and will use specific ssh configuration

$ git remote -v

origin git@github-microsoft: microsoft/microservice-repo.git (fetch)

origin git@github-microsoft: microsoft/microservice-repo.git (push)

Thanks

I’d like to say a big thanks to other writers of the similar articles, honestly during research I did not saved sources. Moreover, the are copyrighted.

Hope this run-book will be useful for others.

Automate all the things. 🤖