but if I have 2 different repositories on different accounts, and the ssh key is only one? Or in this case, you need to spawn n keys for n accounts?
if you created several accounts on one server providing access to git-repositories via ssh-keys (services like github.com, bitbucket.org, etc., or your own servers running gitlab, gitorious, gitolite, etc. .), then you will have to log in on this server with different accounts using different ssh-keys.
if you add the same ssh key to different accounts on the same server, you will get something like this answer: Why are you still asking for a username and password when pushing? - you will be logged in as the user to whom this key will be the first to add.
configuration example for two accounts ( acc1 and acc2 ) on the github.com server.
Creating a few keys that ssh can use (it is called by git to interact with the server) is a snap. create two with the names key1 and key2:
$ ssh-keygen -f ~/.ssh/key1 ... Your identification has been saved in ~/.ssh/key1. Your public key has been saved in ~/.ssh/key1.pub. ... $ ssh-keygen -f ~/.ssh/key2 ... Your identification has been saved in ~/.ssh/key2. Your public key has been saved in ~/.ssh/key2.pub. ...
The ssh-keygen program (among other things) tells you where and under what names it saved the public / secret key pairs. the directory (in this case) you can choose another one, just there ( ~/.ssh ) the ssh program will search for pairs of keys if you do not specify the full path to them.
in order to access the same server ( github.com ) with different keys now, you need to add two sections to ~/.ssh/config :
host gh-acc1 hostname github.com user git identityfile key1 host gh-acc2 hostname github.com user git identityfile key2
what is specified in the host directive (the sections in this file begin with this directive) is an arbitrary string that you can now specify to both the ssh program and the git program.
if you now add ~/.ssh/key1.pub to the first account ( acc1 ), and ~/.ssh/key2.pub to the second one ( acc2 ), you can immediately test it (just make sure that the ~/.ssh and its contents are readable and writable only by you - chmod -R go= ~/.ssh ):
$ ssh -T gh-acc1 Hi acc1! You've successfully authenticated, but GitHub does not provide shell access. $ ssh -T gh-acc2 Hi acc2! You've successfully authenticated, but GitHub does not provide shell access.
as you can see, the server perfectly recognized both accounts.
in order to use this address with the help of the git program, you just need to replace the url of git@github.com with the corresponding string (which you specified in the line with the host directive above).
for example, to clone the repository git@github.com:dockerdemo/apache.git on behalf of the account acc1 :
$ git clone gh-acc1:dockerdemo/apache.git
on behalf of account acc2 :
$ git clone gh-acc2:dockerdemo/apache.git