Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ugultopu/348a2aa782864f0adb25979cba55123c to your computer and use it in GitHub Desktop.
Save ugultopu/348a2aa782864f0adb25979cba55123c to your computer and use it in GitHub Desktop.

Summary

  • 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/967BFF88-A8E1-4EEC-B298-668012E42C03@gmail.com/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 git@bitbucket.org:path/to/the/repository.git
    

Details

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 git@bitbucket.org: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/967BFF88-A8E1-4EEC-B298-668012E42C03@gmail.com/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

Gotchas (OBSOLETE)

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.

Sources

https://superuser.com/questions/232373/how-to-tell-git-which-private-key-to-use#1077869

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment