create different ssh keys according the article Mac Set-Up Git
$ ssh-keygen -t rsa -C "user1@example.com"
$ ssh-keygen -t rsa -C "user2@example.com"
Please refer to github ssh issues for common problems.
For example, 2 keys were created at:
~/.ssh/id_rsa_user1
~/.ssh/id_rsa_user2
Before adding users, you can check existing keys:
$ ssh-add -l
or delete all cached keys:
$ ssh-add -D
Then, add these two keys as following:
$ ssh-add ~/.ssh/id_rsa_user1
$ ssh-add ~/.ssh/id_rsa_user2
Finally, you can check your saved keys:
$ ssh-add -l
$ cd ~/.ssh/
$ touch config
$ editor -a config # use your favorite editor - vim, nano etc...
Then add:
#user1 account
Host github.com-user1
HostName github.com
User user1@example.com
IdentityFile ~/.ssh/id_rsa_user1
#user2 account
Host github.com-user2
HostName github.com
User user2@example.com
IdentityFile ~/.ssh/id_rsa_user2
git clone git@github.com:user1/your_repo.git your_repo
cd your_repo and modify git config as needed, for example:
$ git config user.name "user2"
$ git config user.email "user2@example.com"
editor .git/config
It should have this section:
[remote "origin"]
url = git@github.com-user2:user2/your-repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
Make sure your url =
part has the exact same syntax, note the -user2
in git@github.com-user2
.
or you can have global git config $ git config --global user.name "user2" $ git config --global user.email "user2@example.com"
Like, share & profit!