Post

Handling multiple Github accounts on MacOS

Guideline

1. Creating the SSH keys. For each SSH key pairs:

Go ~/.ssh

Run: ssh-keygen -t rsa -b 4096 -C "[email protected]"

2. Register your keys to the respective GitHub accounts.

Follow these steps to do so.

3. Head back over to the SSH config file at ~/.ssh and amend accordingly to:

1
2
3
4
5
6
7
8
9
10
11
12
13
#user1 account
Host github.com-user1
   HostName github.com
   User git
   IdentityFile ~/.ssh/github-user1
   IdentitiesOnly yes

#user2 account
Host github.com-user2
   HostName github.com
   User git
   IdentityFile ~/.ssh/github-user2
   IdentitiesOnly yes

Replace user1 or user2 with your GitHub usernames/identification-handlers

Example:

1
2
3
4
5
6
7
8
9
10
11
Host github.com-lamngockhuong
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa
   IdentitiesOnly yes

Host github.com-khuongln
   HostName github.com
   User git
   IdentityFile ~/.ssh/github_khuongln_id_rsa
   IdentitiesOnly yes

4. Go ahead to git clone your respective repository

1
git clone [email protected]:user1/your-repo-name.git your-repo-name_user1

Example:

1
2
3
git clone [email protected]:vndevteam/your-repo-name.git your-repo-name_vndevteam
# or
git clone [email protected]:lamngockhuong/dev.git

5. Configure your git identity

Open up local git config using git config --local -e and add:

1
2
3
[user]
   name = user1
     email = [email protected]

6. Ensure your remote url is in the right format e.g: [email protected]:user1/your-repo-name.git your-repo-name_user1

  • You either run git remote set-url origin [email protected]:user1/your-repo-name.git your-repo-name_user1
  • Or amend your remote ssh-url in your local git config file:
1
2
3
[remote "origin"]
       url = [email protected]:user1/your-repo-name.git
       fetch = +refs/heads/*:refs/remotes/origin/*

Now you can git actions (pull/push/fetch…etc) all you like!

References

This post is licensed under CC BY 4.0 by the author.