I have 2 BitBucket accounts with different keys. Here is my ~/.ssh/config
Host company.bitbucket.org
HostName bitbucket.org
User company
IdentityFile ~/.ssh/company
Host personal.bitbucket.org
HostName bitbucket.org
User personal
IdentityFile ~/.ssh/personal
In my repos:
[remote "origin"]
url = company@bitbucket.org:companyworkspace/project1.git
[remote "origin"]
url = personal@bitbucket.org:personal/project2.git
When I use git fetch on each repository, sometime it connects correct, sometime it returns Forbidden.
After debugging, I consider a weird thing.
$ ssh -T company@bitbucket.org
logged in as personal
$ ssh -T company@bitbucket.org
logged in as company
$ ssh -T company@bitbucket.org
logged in as personal
$ ssh -T somethingwrong@bitbucket.org
logged in as personal
Could someone please explain why it is happening?
UPDATED: I misunderstood that I configured ssh correctly, because I am still able to connect git well. The question is about weird behavior when git choose wrong key. However finally, I considered the domain in .git/config must be same the Host in .ssh/config.
Host company.bitbucket // This line should be same git config [...@company.bitbucket:...]
HostName bitbucket.org
User company
IdentityFile ~/.ssh/company