# Work With Multiple Git accounts ## Generate SSH key ### Github Full instruction is [here](https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/). 1. Run command. ```shell ssh-keygen -t ed25519 -C "your_email@organization_domain" ``` 2. Specify filename in `%USERPROFILE%/.ssh/github`. 3. Add generated public SSH key into [Github account](https://github.com/settings/keys). [Instruction](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account). ### Bitbucket Full instruction is [here](https://support.atlassian.com/bitbucket-cloud/docs/set-up-an-ssh-key/) 1. Run command ```shell ssh-keygen -t ed25519 -C "your_email@organization_domain" ``` 2. Specify filename `%USERPROFILE%/.ssh/bitbucket` 3. Add generated public SSH key into [Bitbucket account](https://bitbucket.org/account/settings/ssh-keys/). [Instruction](https://support.atlassian.com/bitbucket-cloud/docs/add-access-keys/) ### Gitlab Full instruction is [here](https://docs.gitlab.com/ee/user/ssh.html) 1. Run command ```shell ssh-keygen -t ed25519 -C "your_email@organization_domain" ``` 2. Specify filename `%USERPROFILE%/.ssh/gitlab` 3. Add generated public SSH key into [Gitlab account](https://gitlab.com/-/profile/keys). [Instruction](https://docs.gitlab.com/ee/user/ssh.html#add-an-ssh-key-to-your-gitlab-account) ## SSH-agent 1. Kill SSH-agent if exists and run in the background ```shell killall ssh-agent; eval "$(ssh-agent)" ``` 2. Add private SSH keys to the ssh-agent ```shell ssh-add ~/.ssh/github ssh-add ~/.ssh/bitbucket ssh-add ~/.ssh/gitlab ``` 3. Optional. Copy SSH keys into the Windows directory or any other WSL ```shell cp -b ~/.ssh/github /mnt/c/Users/%USERPROFILE%/.ssh/ cp -b ~/.ssh/bitbucket /mnt/c/Users/%USERPROFILE%/.ssh/ ``` 4. Test your connection ```shell ssh -T git@github.com ssh -T git@bitbucket.org ssh -T git@gitlab.com ``` ## Configurate `ssh-agent` for multiple SSH keys The next steps should be done in each environment where you need access to Git (Windows, WSLs, etc.) 1. Create config using `nano` ```shell nano ~/.ssh/config ``` 2. Put the next config into `%USERPROFILE%/.ssh/config` ```shell Host github.com HostName github.com User git IdentityFile ~/.ssh/github Host github.com-username HostName github.com User git IdentityFile ~/.ssh/username Host bitbucket.org HostName bitbucket.org User git IdentityFile ~/.ssh/bitbucket Host gitlab.com HostName gitlab.com User git PreferredAuthentications publickey IdentityFile ~/.ssh/gitlab ``` 3. Optional. Copy SSH config into each environment ```shell cp -b ~/.ssh/config /mnt/c/Users/%USERPROFILE%/.ssh/ ``` 4. Set up autostarting `ssh-agent` 1. Update `%USERPROFILE%/.profile` on the next way using: ```shell nano .profile ``` 2. In the end of the file, put the next ```shell if [ "$BASH" ]; then if [ -f ~/.bashrc ]; then . ~/.bashrc eval "$(ssh-agent -s)" fi fi mesg n 2> /dev/null || true ``` ## Cloning repositories using different accounts By default, ```shell git clone git@github.com:organization/remote-repository.git git clone git@bitbucket.org:organization/remote-repository.git git clone git@gitlab.com:organization/remote-repository.git ``` For specific account, ```shell git clone git@github.com-username:organization/remote-repository.git ```