Skip to content

Instantly share code, notes, and snippets.

@yuriitaran
Last active January 25, 2022 20:09
Show Gist options
  • Save yuriitaran/14d5b3f728da94e2bcb158865ac5f58a to your computer and use it in GitHub Desktop.
Save yuriitaran/14d5b3f728da94e2bcb158865ac5f58a to your computer and use it in GitHub Desktop.
Add multiple ssh keys

Multiple SSH Keys settings for different github accounts

Create different public keys

Create different ssh keys according to the article Generating a new SSH key and adding it to the ssh-agent

$ ssh-keygen -t ed25519 -C "your_email@example.com"

Please refer to github ssh issues for common problems.

for instance, you have created the following keys:

~/.ssh/id_ed25519_w
~/.ssh/id_ed25519_p

then, add these two keys as following

$ ssh-add ~/.ssh/id_ed25519_w
$ ssh-add ~/.ssh/id_ed25519_p

you can delete all cached keys before

$ ssh-add -D

finally, you can check your saved keys

$ ssh-add -l

copy your ssh key

$ xclip -sel clip < ~/.ssh/id_ed25519_p.pub

Modify the ssh config

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

Then add

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

# personal account
Host github.com personal-login
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_ed25519_p

Clone you repo and modify your Git config

clone your repo git clone git@github.com:perconal-login/repo_name.git

cd repo_name and modify git config

$ git config user.name "work-login"
$ git config user.email "your_work_email@example.com" 

$ git config user.name "personal-login"
$ git config user.email "your_presonal_email@example.com" 

or you can have global git config

$ git config --global user.name "work-login"
$ git config --global user.email "your_work_email@example.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