How to create and connect to your ssh account is not a problem, but how to do it to work with several remote repositories? So far, I have only been generating them again and fixing them on a githab account. Prompt the right approach to working with multiple repositories at once.

Every time I do this tutor: https://help.github.com/articles/generating-an-ssh-key/

But I feel that I am clearly doing something wrong.


add from comment:

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?

    2 answers 2

    git calls ssh to work with hosts (we are talking about ssh and not raw-git / https) if you set up ssh / keys for each host that you use in your work, then there will be no problems

    those. your main task is not to configure git, but to configure ssh for your google hosts: // ssh-copy-id

    after you can login to the hosts you need, you just set up git:

    git remote add workstation ssh://example.com/home/%USERNAME%/PATH_TO_SOURCE%

    you will be helped by the git remote set-url command git remote set-url and git remote add

    to work with different repositories, for example - type git remote -v - you will see all network (or local but in a different fs directory) repositories

    after you git remote add... bunch of different reps (after git remote add... ,

    you can do for example:

    • git push workstation (push from laptop to home computer, for example),
    • git pull origin (your fork from a github, for example),
    • git fetch && git merge upstream/master (pull and merge with the latest version of your fork master)

    here is almost a good answer for the debit connection git + ssh https://stackoverflow.com/a/36038548/5006740

    instead of GIT_SSH_COMMAND="ssh -v" git clone example you can use GIT_SSH_COMMAND="ssh -vvv" git clone example

    -vvv - even more detailed debug than -v

    • 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? - ddeadlink
    • you can use one key for all hosts (if all your turnips are on a githaba, then you will have to use one key), but if you have a lot of hosts where you can ssh: //, then it’s better to use different keys, so it's just safer . who knows if the key steals and kills all your turnips and your computer, etc. etc. - strangeqargo
    • in the case of different keys, as git gets to my key on the local machine. (1 is in .ssh at the root, and where is the second, third, etc.) - ddeadlink
    • in the case of different keys (and in any case when working via ssh), git will turn to ssh, ssh will see if he has the necessary keys for this host, if not, he curses (in Linux .ssh / config the rules for working with all hosts, but one entry is enough for a githab, since a githab is one host) - strangeqargo
    • Well, this pair is tied to the profile of the gita and the local machine, and if there is a need to use another repository from another profile, how the gita will address its key pair, for another profile - ddeadlink

    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