Skip to content

Instantly share code, notes, and snippets.

@Robokishan
Last active November 19, 2021 07:07
Show Gist options
  • Save Robokishan/1f93a41b0714f2e92cf35797b669e2e8 to your computer and use it in GitHub Desktop.
Save Robokishan/1f93a41b0714f2e92cf35797b669e2e8 to your computer and use it in GitHub Desktop.
Multiple github and bitbucket accounts on same computer

Multiple SSH Keys settings for different github and bitbucket account

create different public key | use same method for bitbucket but hostname and host will change

Create ~/.ssh/github folder

ssh-keygen -t rsa -b 4096 -C 'your-first-email@email.com' -f ~/.ssh/github/personal -q -N ''
ssh-keygen -t rsa -b 4096 -C 'your-work-email@email.com' -f ~/.ssh/github/work -q -N ''
ssh-keygen -t rsa -b 4096 -C 'your-freelance-email@email.com' -f ~/.ssh/github/freelance -q -N ''

ssh-add ~/.ssh/github/personal
ssh-add ~/.ssh/github/work
ssh-add ~/.ssh/github/freelance

then add this keys to appropriate bitbucket and github account

then create

touch ~/.ssh/config

and put this config

Host personal.com
	HostName github.com
	AddKeysToAgent yes
	UseKeychain yes
	User git
	IdentityFile ~/.ssh/github/personal

Host work.com
	HostName github.com
	AddKeysToAgent yes
	UseKeychain yes
	IdentityFile ~/.ssh/github/work
  
Host freelance.com
	HostName github.com
	AddKeysToAgent yes
	UseKeychain yes
	IdentityFile ~/.ssh/github/freelance

then run this commands to check if keys are correct

ssh -T git@personal.com
ssh -T git@work.com
ssh -T git@freelance.com

now if you want to clone which has access of personal account then use this

git clone git@personal.com:<github account org| anything>/<repo>.git

now if you want to clone which has access of personal account then use this

git clone git@work.com:<github account org| anything>/<repo>.git

now if you want to clone which has access of personal account then use this

git clone git@personal.com:<github account org| anything>/<repo>.git

Its cool to git clone from personal.com but actually its just config to use different ssh keys for different github | bitbucket account nothing else. 
This method will support all the git servers mostly. may be creating ssh algorithm will change but other than that everything will be same. feel free to make comments if there is any mistake or method does not work.
dont forget to change git config for local repo don't configure git global 


git config --list 

git config --local

git config --local user.email [Email address you want to use]

git config --local user.name [User name you want to use]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment