In this post, I’ll walk you through configuring and utilizing SSH for Git authentication. If you’re already familiar with SSH’s purpose and importance in Git operations, let’s dive right in.

Scenario 1: Single User

For beginners or those managing a single Git repository account, follow these steps:

  1. Generate SSH Key:
ssh-keygen -t ed25519
  1. Add Public Key to Git Account:

    • Open the generated public key file, copy its content.
    • Add the SSH key to your Git account’s settings.
  2. Test the Configuration:

ssh -T git@github.com

Scenario 2: Multiple Accounts per User

For users managing multiple projects across different Git accounts:

  1. Generate SSH Key with Unique Name.
  2. Add Public Key to Respective Git Accounts.
  3. Configure Hostname and Identity File Mapping:
    • Create or edit the ~/.ssh/config file.
    • Add mappings for each account.
Host x.github.com
    Hostname github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_account_x
  1. Update Remote Address in Git Configuration:
    • Update the remote name in .git/config to match the new configuration.

Scenario 3: Working with Multiple Remote Repositories

If you need to have more than one remote repository for a project:

  1. Add Additional Remote to Project:
    • Create a new repository on your Git account.
    • Use its SSH address to add a new remote with a distinct name.
git remote add remote_name git@github.com:<user>/<project_name>.git
  • Update Remote Hostname if necessary, modify the hostname in .git/config if needed.