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:
- Generate SSH Key:
ssh-keygen -t ed25519
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.
Test the Configuration:
ssh -T git@github.com
Scenario 2: Multiple Accounts per User
For users managing multiple projects across different Git accounts:
- Generate SSH Key with Unique Name.
- Add Public Key to Respective Git Accounts.
- Configure Hostname and Identity File Mapping:
- Create or edit the
~/.ssh/configfile. - Add mappings for each account.
- Create or edit the
Host x.github.com
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_account_x
- Update Remote Address in Git Configuration:
- Update the remote name in
.git/configto match the new configuration.
- Update the remote name in
Scenario 3: Working with Multiple Remote Repositories
If you need to have more than one remote repository for a project:
- 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/configif needed.