I have multiple Git accounts one is my personal use and one is for company use. Both accounts source need to be activated from my laptop. Here I generated two ssh keys like id_rsa.pub,id_benwork_rsa.pub and I configured the config of git as
Host sfsworkdid
HostName bitbucket.org
IdentityFile ~/.ssh/id_rsa
Host workdid
HostName bitbucket.org
IdentityFile ~/.ssh/id_benwork_rsa
So here is my problem: while pushing to any repo git asking the first ssh_key passphrase. Everytime I am changing the user.name in git config as git config user.name "mybitbucketusername". So please tell me how to maintain multiple git accounts with multiple ssh keys in the same system
I tried How to work with multiple ssh keys, Multiple bitbucket accounts but no use
push using multiple account / multiple identity on github / bitbucket is somewhat helpful to reach up to now
So, No - you'll need a separate key for each account. Although you need multiple ssh key pairs for multiple accounts you can configure multiple ssh identities and use via aliases on your machine. You can also just use your username in place of "git" or "hg". Still need separate keys, though.
For instance, you can run an Organization's GitHub account and another one for your personal projects all on the same computer. In this article, you will learn how to use multiple SSH keys for different GitHub accounts. While working with two different GitHub accounts, you must set them up using an SSH key.
You should at this point already have created at least a single default identity. To see if you have a default identity already, list the contents of your .ssh directory. Default identity files appear as a id_encrypt and id_encrypt.pub pair. The encrypt value is either rsa or dsa. Use the ssh-keygen command to create a new identity. In the example below, the identity is named personalid.
$ ssh-keygen -f ~/.ssh/personalid -C "personalid"
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/manthony/.ssh/personalid.
Your public key has been saved in /Users/manthony/.ssh/personalid.pub.
The key fingerprint is:
7a:9c:b2:9c:8e:4e:f4:af:de:70:77:b9:52:fd:44:97 personalid
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| .|
| Eo|
| . S . ..|
| . . o . ... .|
| . = = ..o o |
| . o X ... . .|
| .ooB.o .. |
+-----------------+
If you have multiple Bitbucket accounts, you need to generate a new public/private key pair for each account.
When you have multiple identity files, create a SSH config file mechanisms to create aliases for your various identities. You can construct a SSH config file using many parameters and different approaches. The format for the alias entries use in this example is:
Host alias
HostName bitbucket.org
IdentityFile ~/.ssh/identity
To create a config file for two identities (workid and personalid), you would do the following:
Open a terminal window.
Edit the ~/.ssh/config file. If you don't have a config file, create one.
Add an alias for each identity combination for example:
Host workid
HostName bitbucket.org
IdentityFile ~/.ssh/workid
Host personalid
HostName bitbucket.org
IdentityFile ~/.ssh/personalid
Close and save the file.
Now, you can substitute the alias for portions of the repository URL address as below:
[email protected]:accountname/reponame.git -> git@alias:accountname/reponame.git
To clone a repository with one of multiple SSH identities that you configured, you clone the repo and using your alias in the SSH URL. To try this for yourself, log into Bitbucket and do the following:
This refers to official solution Configure multiple SSH identities for GitBash, Mac OSX, & Linux, It works fine for me!
Edit your ~/.ssh/config file as following !
Host bitbucket.org-yourusername
HostName bitbucket.org
User yourusername
IdentityFile ~/.ssh/yoursshkey
IdentitiesOnly yes
Change your remote git url to have your username before '@bitbucket.org'
git remote add origin [email protected]:company/app.git
or
git remote set-url origin [email protected]:company/app.git
If you have not yet cloned your repository:
git clone [email protected]:company/app.git
An alternative to the ~/.ssh/config
method above is to specify the configuration variable core.sshCommand
in the clone command itself. For example,
git clone --config core.sshCommand='ssh -i/home/username/.ssh/id_ed25519' [email protected]:the_best/awesome_repo.git
This will set the local repository configuration value and make subsequent push/pull commands 'just work'.
$ git config --local --get core.sshCommand
ssh -i/home/username/.ssh/id_ed25519
This is supported in git versions 2.10 and later.
After searching a lot on the web and with the community help I figured out how to configure 2 differents bitbuckets account on my Mac - MacOS Monterey.
Suppose that you have 2 bitbucket accounts witch usernames are username1 and username12.
ssh-keygen -f ~/.ssh/username1-Bitbucket
ssh-keygen -f ~/.ssh/username2-Bitbucket
eval $(ssh-agent)
Create:
touch ~/.ssh/config
open ~/.ssh/config
Edit:
open ~/.ssh/config
The ~/.ssh/config file should look like this:
Host username1-Bitbucket
HostName bitbucket.org
User username1
IdentityFile ~/.ssh/username1-Bitbucket
Host username2-Bitbucket
HostName bitbucket.org
User username2
IdentityFile ~/.ssh/username2-Bitbucket
Host *
UseKeychain yes
AddKeysToAgent yes
PreferredAuthentications publickey
IdentityFile ~/.ssh/username1-Bitbucket
IdentityFile ~/.ssh/username2-Bitbucket
IdentitiesOnly yes
PreferredAuthentications keyboard-interactive,password
ssh-add --apple-use-keychain ~/.ssh/username1-Bitbucket
ssh-add --apple-use-keychain ~/.ssh/username2-Bitbucket
pbcopy < ~/.ssh/username1-Bitbucket.pub
pbcopy < ~/.ssh/username1-Bitbucket.pub
ssh -T [email protected]
Clone your repositories using ssh and cd on the terminal to the repository folder.
Check the reposiroties remote url:
git remote -v
You'll get information like this one:
[email protected]:companyUserName/repositoryName.git
git remote set-url origin username1@username1-Bitbucket:username1/repositoryName.git
git remote set-url origin username2@username2-Bitbucket:username2/repositoryName.git
So we changed [email protected]:companyUserName/repositoryName.git
to username1@username1-Bitbucket:username1/repositoryName.git
git
-> username1
or username2
, the username of the accounts.
bitbucket
-> username1-Bitbucket
or username2-Bitbucket
, the host alias of the config file.
companyUserName
-> It's the username of the account that holds the repository, in our case the repositories are owned by username1 or username2.
repositoryName
-> The name of the repository.
git checkout -b newBranch
git push origin newBranch
If everything works correctly on both repositories, you are done!
Hope that this solution works for every Mac user.
That's all folk!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With