Skip to content

Instantly share code, notes, and snippets.

@johnwatsondev
Last active November 5, 2015 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnwatsondev/3d1c42fe7aa3b6a1ab88 to your computer and use it in GitHub Desktop.
Save johnwatsondev/3d1c42fe7aa3b6a1ab88 to your computer and use it in GitHub Desktop.
Multiple SSH keys for different github accounts

Multiple SSH Keys settings for different github account

Create different public key

create different ssh key according the article Mac Set-Up Git

$ ssh-keygen -t rsa -C "your_email@youremail.com" -f account_1
$ ssh-keygen -t rsa -C "your_email@youremail.com" -f account_2

Please refer to github ssh issues for common problems.

for example, 2 keys created at:

~/.ssh/account_1
~/.ssh/account_2

first you can delete all keys before

$ ssh-add -D

then, add these two keys as following

$ ssh-add ~/.ssh/account_1
$ ssh-add ~/.ssh/account_2

finally, you can check your saved keys

$ ssh-add -l

Modify the ssh config

$ cd ~/.ssh/
$ touch config
$ vi config

Then added

#account_1
Host github-account-1
  HostName github.com
  User git
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/account_1
  IdentitiesOnly yes

#account_2
Host github-account-2
  HostName github.com
  User git
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/account_2
  IdentitiesOnly yes

Test SSH connect

$ ssh -T git@github-account-1
$ ssh -T git@github-account-2

Clone you repo and modify your Git config

clone your repo

git clone git@github-account-1:user1's github username/project.git project1

...or for an existing working directory

git remote set-url origin git@github-account-1:user1's github username/project.git

cd project1 and modify git config

$ git config user.name "user1"
$ git config user.email "user1@gmail.com" 

or you can have global git config

$ git config --global user.name "user1"
$ git config --global user.email "user1@gmail.com" 

then use normal flow to push your code

$ git add .
$ git commit -m "your comments"
$ git push

Credit:

http://stackoverflow.com/questions/3225862/multiple-github-accounts-ssh-config https://gist.github.com/RichardBronosky/dc0ced21d6dc7be7d196
http://blog.chinaunix.net/uid-20749137-id-718749.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment