cft

How to use multiple Github accounts on same machine via SSH

Learn to use multiple github accounts with ssh, without having to switcha accounts or re type passwords over and over,


user

Pavittar Singh

a year ago | 2 min read

When I started using Github, I used Github Desktop for sometime because that felt easy. Down the line, I learnt using git via CLI. And, I haven't touched the electron tool back ever. But, I was still using Github via HTTP vs SSH.

I fell in love with ssh when I first discovered it. Once setup you can use ssh to work with multiple repositories under same account. The only downside is the initial setup that might feel complicated if you are using it for the very first time. However, it saves you from the pain of password/token based authentication (...painful).

If you haven't used ssh before, these two articles would help you set up pretty quick.

  1. Generating a new SSH Key
  2. Adding the Generated Key to your Github Account

So far so good, this works good for a single account. What if you have to use multiple accounts? Personal and Work?

  1. Use ssh-keygen to start creating your new ssh key. Note, this email should be same as the one you use with your Github account.
ssh-keygen -t rsa -C "youremail@domain.com"

2. Give this ssh-key a different name, so that it does not conflict/overwrite existing keys. All existing ssh keys can be found under ~/.ssh folder. I named my own as "pavittarxnoauth".

Enter file in which to save the key (/home/pavittarx/.ssh/id_rsa): pavittarxnoauth

3. In the next steps, provide a password or simply hit enter if you do not want to add any passwords while authentication.

4. Check if the key has been successfully created. If you cannot find the key in .ssh folder. Repeat steps 1-3.

ls ~/.ssh

5. Start your ssh-agent if you have not already.

eval ssh-agent -s

6. Add the key to your ssh agent.

ssh-add ~/.ssh/pavittarxnoauth

7. Navigate to ~/.ssh directory.

cd ~/.ssh

8. Add following entry to the config file. You can also create one if it doesn't exist. Replace, pavittarxnoauth with your key-file name. Also, replace github-noauth to anything as per your preference.

Host github-noauth
HostName github.com
User git
IdentityFile ~/.ssh/pavittarxnoauth

*Indentation is needed.

9. Add the ~/.ssh/<yourkeyfilename.pub> to your Github account as stated here.

10. Clone your repo with ssh, or change the git remote and git config for the repository.

git clone git@github-noauth:<your-github-username>/<your-git-repo

For existing repo, that exists locally,

1. Remove existing remote origin.

git remote remove origin

2. Add new remote origin

git remote add origin git@github-noauth:<github-username>/<git-repo>

3. Change git config, for this particular repository

git config --local user.email "your-github-email@domain.com"
git config --local user.name "github-username"

Finally, use git as you would usually do.

This procedure will work when trying to add multiple keys, with same or different accounts to Github. Also, the same is reproducable for other git providers such as Bitbucket/ Gitlab.

Upvote


user
Created by

Pavittar Singh


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles