Skip to content

Instantly share code, notes, and snippets.

@socmia
Forked from oLeVanLong/multiple_ssh_setting.md
Last active May 13, 2020 02:51
Show Gist options
  • Save socmia/4fa3b47b478bcd71a85bdcca51f7b7de to your computer and use it in GitHub Desktop.
Save socmia/4fa3b47b478bcd71a85bdcca51f7b7de 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"

Please refer to github ssh issues for common problems.

for example, 2 keys created at:

~/.ssh/id_rsa_home
~/.ssh/id_rsa_work

then, add these two keys as following (Đoạn này có thể không add được có thể bỏ qua)

$ ssh-add ~/.ssh/id_rsa_home
$ ssh-add ~/.ssh/id_rsa_work

you can delete all cached keys before

$ ssh-add -D

finally, you can check your saved keys

$ ssh-add -l

Modify the ssh config

$ cd ~/.ssh/
$ touch config
$ subl -a config

Then added

#home account
Host github.com-home
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_home

#work account
Host github.com-work
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_work

Clone you repo and modify your Git config

Nếu đoạn này khong clone được thì xuống dưới set global config

clone your repo git clone git@github.com-home:accountname/gfs.git gfs_work

cd gfs_work (Project vừa clone ở trên, để edit file config) and modify git config

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

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

Đoạn này config để có thể clone được

or you can have global git config $ git config --global user.name "work" $ git config --global user.email "work@gmail.com"

then use normal flow to push your code

$ git add .
$ git commit -m "your comments"
$ git push
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment