We are continually encountering issues with your global definitions while using git services such as Github, Bitbucket, and Gitlab. It will also be inconvenient to enter your git environment information in each project over and again. Whether you assign ssh to each of these services individually or utilize several sshs in the same service. This can easily avoided with ssh/config.

Defining GIT Information on a Single Project Basis

First, let's have a look at the method we utilize in every project. Normally, you define local git information rather than global information in each directory of your git project and do the following procedures.

git config user.name "your_username"
git config user.email "your_email@domain.com"

However, this is a time-consuming and error-prone procedure. It will also necessitate dealing with the app password.

Instead, by generating SSH keys, you may assign these SSH keys to your associated services and manage them effortlessly.

Generate ED25519 SSH Keys

Git services now recommend using more performant and secure algorithms such as ED25519 instead of RSA. RSA will soon be completely removed from these systems. Therefore, let's generate our SSH keys with ED25519 and upload them to the relevant services;

ssh-keygen -t ed25519 -C "name_surname@gmail.com" -f "~/.ssh/id_ed25519_github_name_surname_gmail"
ssh-keygen -t ed25519 -C "hi@name_surname.com" -f "~/.ssh/id_ed25519_github_hi_name_surname"
ssh-keygen -t ed25519 -C "name_surname@yandex.com” -f "~/.ssh/id_ed25519_bitbucket_name_surname_yandex"

 

Here I created 3 different ssh for 3 different git accounts. You can create as many as you need. You can change the relevant nomenclature according to you.

Manage keys from .ssh/config

Create a config file with nano ~/.ssh/config or continue typing into it if the config file exists. This file allows you to specify which ssh file to use and when.

We will manage this config file with two different variations below. The first of these is the method where you enter an account with an e-mail for each service.

Single SSHs for Github and Bitbucket

Here, two accounts are defined in two different services. In this way, we will not need to specify this when making a request to the repo specifically.

  • Github: Personal Account (name_surname@gmail.com)
  • Bitbucket: Work Account (name_surname@yandex.com)
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_github_name_surname_gmail
  IdentitiesOnly yes

Host bitbucket.org
  HostName bitbucket.org
  User git
  IdentityFile ~/.ssh/id_ed25519_bitbucket_name_surname_yandex
  IdentitiesOnly yes

 

Different SSH keys are assigned to host entries in this /.ssh/config file. Each SSH key corresponds to a separate service (such as GitHub or Bitbucket). The User field will nearly always be "git" because we provide Git activities under this username in these services.

The IdentitiesOnly yes line disables identities other than the one supplied in the IdentityFile. This stops the system from trying to use another key by accident and ensures that the proper key is utilized.

Multiple SSHs for Github and Bitbucket

For example, we'll assume you're logged into two distinct Github accounts and one on Bitbucket. In this situation, we should make a minor update to the host names and use these names to manage our clone and push requests.

  • Github: Personal Account (name_surname@gmail.com)
  • Github: Work Account (hi@name_surname.com)
  • Bitbucket: Work Account (name_surname@yandex.com)
Host github-namesurname
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_github_name_surname_gmail
  IdentitiesOnly yes

Host github-hinamesurname
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_github_hi_name_surname
  IdentitiesOnly yes

Host bitbucket
  HostName bitbucket.org
  User git
  IdentityFile ~/.ssh/id_ed25519_bitbucket_namesurname_yandex
  IdentitiesOnly yes


Now when running git commands, you can use github-namesurname: or github-hinamesurname to specify your GitHub account and bitbucket for Bitbucket. For example:

git clone git@github-namesurname:workspace/reponame.git
git clone git@github-hinamesurname:workspace/reponame.git
git clone git@bitbucket:workspace/reponame.git


As you can see, we have made changes to all service names except bitbucket so that it goes to the service related to the conditions we have determined.

How Can I Add SSH Keys to Services Like Github, Bitbucket, Gitlab?

Finally, note that after making these settings, each key must be added to the respective service (GitHub and Bitbucket). On the Settings page of each service, in the SSH keys section, you can find the relevant PUB files (~/.ssh/id_ed25519_github_name_surname_gmail.pub and ~/.ssh/id_ed25519_bitbucket_name_surname_yandex.pub). Copy these keys and paste them into each service.

In Bitbucket, you can add it to the SSH Keys page under Personal Settings. You can access it at bitbucket.org/account/settings/ssh-keys/

You can add it from the SSH Keys page under Settings on Github.
You can access it at github.com/settings/keys

Check Connection to Related Git Service

To make sure everything is done correctly, you can run the following commands:

ssh -T git@github.com
ssh -T git@bitbucket.org

 
Bu komutlar, ilgili servislerle bağlantıyı test eder. Her iki durumda da "OK" gibi bir başarı mesajı almalısınız.

Known Bugs

Macos ~Tilde Problem

On Macos, the ~ (tilde) may be causing problems. So for the creation process, you can do it by giving the full path as follows;

ssh-keygen -t ed25519 -C "name_surname@gmail.com" -f “/Users/username/.ssh/id_ed25519_github_name_surname"

..