Skip to content

About the author of Daydream Drift

Tomasz Niezgoda (LinkedIn/tomaszniezgoda & GitHub/tniezg) is the author of this blog. It contains original content written with care.

Please link back to this website when referencing any of the materials.

Author:

Multiple GitHub Accounts? No Problem

Published

I work for companies and myself under different accounts on GitHub. They give me access to private projects and provide a grouping of various kinds of work which I enjoy having.

But GitHub and Git don't provide a way to easily use multiple accounts. Many desktop Git clients do. However, only within their own environment.

What I found over time, is that using the terminal for Git is more reliable (I'm looking at you, SourceTree) and faster. I also make fewer errors when using it, in comparison to a dedicated client.

So, here are some solutions making working as multiple users on Git projects easier.

Accessing A Repo As A Specific Person

Add the following setup to ~/.ssh/config:

Host github.com-SUFFIX_1
    HostName github.com
    User GITHUB_USERNAME_1
    IdentityFile "PATH_TO_USER_1_PRIVATE_KEY"

Host github.com-SUFFIX_2
    HostName github.com
    User GITHUB_USERNAME_2
    IdentityFile "PATH_TO_USER_2_PRIVATE_KEY"

And simply git clone github.com-SUFFIX_1:... or git clone github.com-SUFFIX_2:... instead of git clone github.com:.... Git will save the suffixed remote address in the cloned repo, so fetching, pushing and other Git operations will work exactly the same way.

For an existing repository, modify Git remotes using git remote rm REMOTE_NAME and git remote add REMOTE_NAME github.com-SUFFIX_1:....

Committing As A Specific Person

When trying to use multiple GitHub accounts on a single development machine, it's very easy to mistakenly commit or push changes as the wrong user. That's because Git stores a default user in the system and GitHub identifies users by SSH key, username and e-mail.

Using the below command along with custom remotes from ~/.ssh/config ensures the correct meta data is attached to commits. I have this command setup as a snippet in Alfred, so It's easier to quickly input into a terminal window.

GIT_COMMITTER_NAME='GITHUB_USERNAME' \
GIT_COMMITTER_EMAIL='GITHUB_EMAIL' \
git commit --author 'GITHUB_USERNAME <GITHUB_EMAIL>'

GitHub discerns between authors and committers, thus the double username and e-mail.