-1

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
Duc Le
  • 347
  • 1
  • 7
  • 12
  • 1
    Possible duplicate of [push using multiple account / multiple identity on github / bitbucket](https://stackoverflow.com/questions/18725033/push-using-multiple-account-multiple-identity-on-github-bitbucket) – phd May 31 '19 at 10:17
  • https://stackoverflow.com/search?q=%5Bgit%5D+%5Bssh%5D+multiple+accounts – phd May 31 '19 at 10:17
  • @phd I misunderstood about the configuration and thought it's different case with above link. – Duc Le Jun 03 '19 at 04:11

1 Answers1

1

you need to edit your git config

[remote "origin"]
url = git@personal.bitbucket.org:personal/repo.git

and for the other one replace the user and URL to Company.

at the end add this to your ssh config:

IdentitiesOnly=yes
LinPy
  • 16,987
  • 4
  • 43
  • 57
  • I just updated my question. I have already had git remote config as above. I am still able to fetch code from Bitbucket. However sometime it returns Forbidden because of different user. I just added "IdentitiesOnly yes" to each host config as your recommend but it still returns Forbidden: `Host company.bitbucket.org HostName bitbucket.org User company IdentitiesOnly yes IdentityFile ~/.ssh/company` – Duc Le May 31 '19 at 08:53
  • you need to use `personal.bitbucket.org` as a host in git config not bitbucket.org – LinPy May 31 '19 at 08:55