Note: Original blog post HERE.
Create a key for each of your accounts
To generate a new key pair simply run this command in the ~/.ssh/ folder:
Note: Original blog post HERE.
To generate a new key pair simply run this command in the ~/.ssh/ folder:
ssh-keygen -t rsa -C "user1" -f "user1"
The -C option is a comment to help identify the key. The -f option specifies the file name. Repeat the above for each Bitbucket account you want to use.
To add a public key to a Bitbucket account, you need to go to the Bitbucket Settings Screen. SelectSSH Keysin the left side menu and clickAdd key.
In ~/.ssh/ create a file called config with contents based on this:
#user1 account
Host bitbucket.org-user1
HostName bitbucket.org
User git
IdentityFile ~/.ssh/user1
IdentitiesOnly yes
#user2 account
Host bitbucket.org-user2
HostName bitbucket.org
User git
IdentityFile ~/.ssh/user2
IdentitiesOnly yes
Replace user1 or user2 with your Bitbucket usernames.
If you don’t have a local copy of your repo, you have to run the following command to clone a Bitbucket repository:
git clone git@bitbucket.org-user1:your-repo-name.git
If you already have a local copy, you’ll need to update the origin:
git remote set-url origin git@bitbucket.org-user1:your-repo-name.git
Now go to the local Git repo that you want to configure and enter:
git config user.name "user1"
git config user.email "user1@example.com"
Where user1 matches the values you used earlier in your ssh config.
You can now git push as normal and the correct key will automatically be used.