Skip to content

Instantly share code, notes, and snippets.

@the-glima
Forked from jexchan/multiple_ssh_setting.md
Last active May 24, 2021 03:27
Show Gist options
  • Save the-glima/37d90c5060f8d9dd2e5bdb6611c416f5 to your computer and use it in GitHub Desktop.
Save the-glima/37d90c5060f8d9dd2e5bdb6611c416f5 to your computer and use it in GitHub Desktop.
Multiple SSH keys GitHub setup

Multiple SSH Keys settings for different github account

create different public key

Make sure you have .ssh folder created:

$ mkdir ~/.ssh
$ chmod 700 ~/.ssh

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

$ ssh-keygen -t rsa -C "your_email@youremail.com" -f "key-name"

Please refer to github ssh issues for common problems.

for example, 2 keys created at:

~/.ssh/id_rsa_github
~/.ssh/id_rsa_bitbucket

then, add these two keys as following

Make sure that the ssh-agent is running:

eval `ssh-agent`
$ ssh-add ~/.ssh/id_rsa_github
$ ssh-add ~/.ssh/id_rsa_bitbucket

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

# GitHub account
Host github.com
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_github

# Bitbucket account
Host bitbucket.com
	HostName bitbucket.com
	User git
	IdentityFile ~/.ssh/id_rsa_bitbucket

Setup the SSH in the service of your choice:

After that, check if everything is ok:

  • GitHub: ssh -T git@github.com
  • Bitbuket: ssh -T git@bitbucket.org

Set up your git user and email

Globally

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

By project

$ git config user.name "foo"
$ git config user.email "foo@email.com" 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment