Create different ssh keys for each git account
$ ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to [email protected]" -f ~/.ssh/[email protected]_id_rsa
$ ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to [email protected]" -f ~/.ssh/[email protected]_id_rsa
$ ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to [email protected]" -f ~/.ssh/[email protected]_id_rsa
$ ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to [email protected]" -f ~/.ssh/[email protected]_id_rsa
If you would like to use a passphrase then remove -N ""
from the command, you will then be prompted for you pass-phrase.
This will create the following files
~/.ssh/[email protected]_id_rsa
~/.ssh/[email protected]_id_rsa
~/.ssh/[email protected]_id_rsa
~/.ssh/[email protected]_id_rsa
Please refer to github ssh issues for common problems.
You can add your keys to memory (give to ssh-agent) using the ssh-add
command,
# run agent if it is'nt already
$ eval `ssh-agent -s`
$ ssh-add ~/.ssh/[email protected]_id_rsa
$ ssh-add ~/.ssh/[email protected]_id_rsa
$ ssh-add ~/.ssh/[email protected]_id_rsa
$ ssh-add ~/.ssh/[email protected]_id_rsa
or modify ~/.ssh/config to specify that keys should be automatically added to a running ssh-agent
To list your added keys
$ ssh-add -l
to delete all cached keys with
$ ssh-add -D
Make sure ~/.ssh exists and is writable
$ mkdir -p $HOME/.ssh
$ chmod 0700 $HOME/.ssh
Make sure ~/.ssh/config exists and open in your text editor (subl)
$ cd ~/.ssh/
$ touch config
$ subl -a config
Then add the following
#Set Git User Domains
Host *-github.com *-bitbucket.org *-bitbucket.com
User git
#IdentityFile
Host tarranjones-*
IdentityFile ~/.ssh/tarranjones@%h_id_rsa
Host otherusername-*
IdentityFile ~/.ssh/otherusername@%h_id_rsa
#Hostnames
Host *-github.com
Hostname github.com
Host *-bitbucket.com *-bitbucket.org
Hostname bitbucket.org
Host *
Protocol 2
UseKeychain yes
AddKeysToAgent yes
IdentitiesOnly yes
I recommend setting your global config to match your most frequently used account details
$ git config --global user.name "tarranjones"
$ git config --global user.email "[email protected]"
Having the same username and email address for all your primary accounts across different git hosting providers is something worth thinking about. Your user config would have to be changed a lot less.
clone your repo
git clone tarranjones-github.com:username/repo.git
...or for an existing working directory
git remote set-url origin tarranjones-github.com:username/repo.git
you should set your user config for repositories where details differ from your global config details.
cd repo and modify git config
$ git config user.name "otherusername"
$ git config user.email "[email protected]"
then use normal flow to push your code
$ git add .
$ git commit -m "your comments"
$ git push
Heres a little helper function
usage: ssh [user@]host]
function ssh_keygen(){
ssh-keygen -t rsa -b 4096 -N "" -C "$USER@$HOSTNAME to $1" -f ~/.ssh/"$1_id_rsa"
}
$ ssh_keygen [email protected]
further reading
alias to switch user.email http://stackoverflow.com/a/33079036/2273611
Another related article in Chinese