1

I use my company laptop for company stuff but sometime I would also want to use it to code my own side project. Now my git is configured to use company's git account, how can I use multiple accounts in one device? Note that my company and my side project is using bitbucket too.

Or I should just create another user in mac, one for company stuff one for personal stuff?

  • So, your question is actually about multiple Bitbucket accounts, right? "git account" is not really a thing, so it's rather confusing to use that term. – Frax May 12 '18 at 09:44

1 Answers1

2

It depends on how you access the remote repo

  • https: make sure add your personal user account in the remote repo URL: https://me@bitbucket.org/me/myrepo.
    Then use a credential helper to cahce the credentials (login/password).
  • ssh: you can configure multiple private keys, one linked to a professional account, one linked to a personnal account, all referenced in ~/.ssh/config.
    See "MacOS Terminal: how to use a seccond ssh key?" as an example.

Make sure, within your personal repo, to set the right user.name/user.email

cd /my/repo
git config user.name myName
git config user.email myPersonal@Email.com

That does not influence the authentication, but matters in order for your commits to reflect "who" commit them.

Nathan Ton
  • 41
  • 1
  • 6
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • oh that's great, use my personal account in remote repo, that is it? I don't mind the user.name git config because I used the same name in 2 different places. –  May 12 '18 at 09:37
  • @Melissa92 The user.email is actually important to separate your accounts in term of commits (not in term of authentication). – VonC May 12 '18 at 09:38
  • @Melissa92 But yes, you can use different account per repo. – VonC May 12 '18 at 09:38
  • The important part is adding username to bitbucket urls, without it ssh may use random keys and confuse the identities. [Example in Atlassian doc](https://confluence.atlassian.com/bitbucket/set-up-additional-ssh-keys-271943168.html#SetupadditionalSSHkeys-ssh1SetupadditionalSSHkeysforGit) (referenced in linked question). – Frax May 12 '18 at 09:49
  • @Frax Do you mean https? SSH does not use any other username than 'git': git@bitbucket.org. – VonC May 12 '18 at 09:50
  • @Frax You would see username used in SSH URL only for mercurial repo on BitBucket, not Git repo. – VonC May 12 '18 at 09:51
  • Well, the Bitbucket doc I linked disagrees with you, giving an example of `git remote set-url origin @bitbucket.org:teamsinspace/bitbucketspacestation.git`. – Frax May 12 '18 at 10:16
  • @Frax Agreed, indeed. That would not be the best practice. To manage multiple keys, use ~/.ssh/config. – VonC May 12 '18 at 10:21
  • I don't follow. How is it relevant? If you have two keys, ssh will use any of them, so you can't control as which user you are identified by Bitbucket without saying so explicitly. – Frax May 12 '18 at 10:33
  • @Frax Yes you can, please read https://stackoverflow.com/questions/17142566/macos-terminal-how-to-use-a-seccond-ssh-key/17142623#17142623. It is relevant, because that is how you manage multiple keys with SSH, for BitBucket or any other remote SSH service. – VonC May 12 '18 at 10:34
  • Both the username@bitbucket.org approach and the ~/.ssh/config trick work, since each SSH key will only work with a single Bitbucket account (even if your client attempts to use them all). username@bitbucket.org is a bit easier to keep separate, IMHO, but ~/.ssh/config also works on other providers. – Jim Redmond May 14 '18 at 17:42