When we need to connect to multiple GIT accounts from same provider an extra configuration need to be executed.
How to be logged in web browser?
We can use different browsers or Chrome profiles for each GIT account.
In my opinion the best solution is Firefox Multi-Account Containers. Cookies are separated by container, so we can easily sign in to same domain with different users in each container. In addition this addon allows us to open tabs with different container next to each other with keyboard shortcut.
How to manage connection in GIT?
Imagine that we need to use 2 Bitbucket accounts. First create separate SSH keys:
- ssh-keygen -f ~/.ssh/id_rsa_demo_one
- ssh-keygen -f ~/.ssh/id_rsa_demo_two
Then go to web browser and configure SSH keys for GIT accounts. Simply by copying .pub key file.
Next we need to use those keys. There are several ways to do it.
Prefixes in global SSH configuration
Create custom named connections in SSH config file:
Host demo_one.bitbucket.org
HostName bitbucket.org
User YOUR_BITBUCKET_USER_2
IdentityFile ~/.ssh/id_rsa_demo_one
IdentitiesOnly yes
Host demo_two.bitbucket.org
HostName bitbucket.org
User YOUR_BITBUCKET_USER_2
IdentityFile ~/.ssh/id_rsa_demo_two
IdentitiesOnly yes
The IdentitiesOnly is very important here.
https://unix.stackexchange.com/a/494485/198149
Then we need to change domain name to previously defined Host value.
- when cloning repository: git clone git@demo_one.bitbucket.org:private/repo-name.git
- when repository is already cloned on local we need to check remote GIT remote with git remote -v command and the set valid new URL: git remote set-url origin git@demo_one.bitbucket.org:private/repo-name.git
- when adding remote to existing repository: git remote add origin git@demo_one.bitbucket.org:private/repo-name.git
You can use following script to automatically change it in Your config:
find . -type file -name config -exec sed -i '' 's/bitbucket.org:/demo_one.bitbucket.org:/g' {} \;
Local GIT configuration
You can configure SSH identity file in local repository, so all GIT commands will use defined configuration. Just run git config core.sshCommand "ssh -i ~/.ssh/id_rsa_demo_one" command.
You can not configure SSH identity file in local repository, because it will be ignored during connection. SSH will try all keys , so first success will be used. If Your file is not first on list, You will not be authorized to with wanted credentials.
You can also load multiple keys during SSH authentication.