-
Create a new SSH key pair by running
ssh-keygen
. -
Add an entry like the following to your
~/.ssh/config
file:# The value of the `Host` below (which is the "title" of the entry) # can be anything. However, it is better to use a descriptive name # like "bitbucket-yourCompanyName" instead of the less descriptive # example "someNameThatYouChoose" below. Host someNameThatYouChoose HostName bitbucket.org User git IdentityFile ~/.ssh/the-name-that-you-gave-to-your-key-pair-while-generating-it-in-the-previous-step # Following is required. More information at: # https://public-inbox.org/git/[email protected]/T # https://git-scm.com/docs/gitfaq#multiple-accounts-ssh IdentitiesOnly yes
-
Now, you can use it as:
git clone someNameThatYouChoose:path/to/the/repository.git
instead of:
git clone [email protected]:path/to/the/repository.git
Let's say that you have an account on BitBucket and you want to use SSH to connect, instead of HTTPS. How can you accomplish this?
Well, first of all, you need to create an SSH key pair. After doing so, you need to add the public key to your BitBucket account. After that, you are able to use BitBucket with SSH without a problem.
Now, let's say that you started a job and your job uses BitBucket as well. Since it is a job, you cannot use your personal BitBucket account. You have to use the BitBucket account that the company has created for you. However, you want to use SSH to access to BitBucket with your company BitBucket account as well. How can you do this?
Well, first of all, you need to add your public key to the company BitBucket account as well. So you try it. However, BitBucket gives you an error:
Someone has already added that SSH key.
What should you do? Well, apparently you need to create a new SSH key pair. To do so, you launch a terminal window and run ssh-keygen
. After entering the required parameters (which is mainly just the name of the files that will hold the key pair), your new key pair is created.
Now, you add the public key to your company BitBucket account. After adding it successfully, you run a Git command (such as git clone
) on the terminal like:
git clone [email protected]:path/to/the/repository.git
However, you get:
Forbidden
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
What happened? Well, Git used your personal SSH key pair, instead of using the SSH key pair that you created for your company BitBucket account. In order for Git to use the SSH key pair that you created for your company BitBucket account, you need to add an entry to the ~/.ssh/config
file:
# The value of the "Host" below can be anything. However, it is
# better to use something descriptive.
Host bitbucket-yourCompanyName
HostName bitbucket.org
User git
IdentityFile ~/.ssh/the-name-that-you-gave-to-your-key-pair-while-generating-it
# Following is required. More information at:
# https://public-inbox.org/git/[email protected]/T
# https://git-scm.com/docs/gitfaq#multiple-accounts-ssh
IdentitiesOnly yes
Now, you can use it as:
git clone bitbucket-yourCompanyName:path/to/the/repository.git
THIS SECTION IS OBSOLETE. JUST ADD IdentitiesOnly yes
TO THE ENTRY AT ~/.ssh/config
. MORE INFORMATION AT HERE AND HERE.
There seems to be a bug with Git that when you have a key in the SSH agent, regardless of what you declare in the ~/.ssh/config
file as the IdentityFile
, Git seems to be using the key that is in the SSH agent, even though the key ("IdentityFile") declared in ~/.ssh/config
is different.
Hence, as a workaround, make sure that you either don't have any keys in your SSH agent, or make sure to add the SSH key ("IdentityFile") that you declared in ~/.ssh/config
to the SSH agent as well.
https://superuser.com/questions/232373/how-to-tell-git-which-private-key-to-use#1077869