DEV Community

Appy
Appy

Posted on

Mastering Multi-Account Git: Seamless SSH Key Management on a Single Machine

Image description

Introduction

Managing multiple Git accounts on a single machine is a common challenge for developers juggling work and personal projects. With SSH keys, you can effortlessly switch between accounts without compromising security or convenience. In this guide, we'll walk you through the process of setting up and managing two Git accounts using SSH keys, helping you streamline your workflow and keep your Git identities neatly organized.

For beginners:

  • Git: A version control system that tracks changes in your code over time.
    • Git is like a magical notebook that remembers all the changes you make to your coloring book. It helps you go back in time to see your old drawings!
  • SSH (Secure Shell): A protocol that provides a secure way to access a computer over an unsecured network.
    • SSH is like a secret handshake between computers. It makes sure only your friend's computer can talk to your computer, keeping all the other computers out.

Walkthrough

Here’s a detailed walkthrough on setting up Git for two users, one global and one local, using SSH keys. This guide will cover:

  • Generating SSH keys for each account
  • Configuring SSH keys for each account
  • Setting up global and local Git user configurations
  • Cloning and working with repositories from each account
  • Viewing SSH keys

1. Generating SSH Keys for Each Account

First, generate an SSH key for each GitHub account.

For the First Account:

ssh-keygen -t rsa -C "email_for_first_account@example.com" -f ~/.ssh/id_rsa_first_account
Enter fullscreen mode Exit fullscreen mode

For the Second Account:

ssh-keygen -t rsa -C "email_for_second_account@example.com" -f ~/.ssh/id_rsa_second_account
Enter fullscreen mode Exit fullscreen mode

2. Configuring SSH Keys

Add the generated SSH keys to the SSH agent and configure the SSH configuration file.

Start the SSH Agent:

ssh-add ~/.ssh/id_rsa_first_account
ssh-add ~/.ssh/id_rsa_second_account
Enter fullscreen mode Exit fullscreen mode

Configure ~/.ssh/config:

nano ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

Add the following configuration:

# Configuration for the first account
Host github.com-first-account
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_first_account

# Configuration for the second account
Host github.com-second-account
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_second_account
Enter fullscreen mode Exit fullscreen mode

3. Setting Up Global and Local Git User Configurations

Global Configuration:

Set the global Git user configuration to the first account:

git config --global user.name "First Account Name"
git config --global user.email "email_for_first_account@example.com"
Enter fullscreen mode Exit fullscreen mode

Local Configuration:

For each repository you clone for the second account, set the local Git user configuration:

cd /path/to/second-account-repo
git config user.name "Second Account Name"
git config user.email "email_for_second_account@example.com"
Enter fullscreen mode Exit fullscreen mode

4. Cloning and Working with Repositories

Cloning a Repository for the First Account:

Use the following command to clone a repository for the first account:

git clone git@github.com-first-account:username/repo.git
Enter fullscreen mode Exit fullscreen mode

Cloning a Repository for the Second Account:

Use the following command to clone a repository for the second account:

git clone git@github.com-second-account:username/repo.git
Enter fullscreen mode Exit fullscreen mode

5. Viewing SSH Keys

To view your SSH keys:

cat ~/.ssh/id_rsa_first_account.pub
cat ~/.ssh/id_rsa_second_account.pub
Enter fullscreen mode Exit fullscreen mode

Copy the content of each key and add it to the corresponding GitHub account under Settings > SSH and GPG keys.


Conclusion

By following this guide, you will be able to manage multiple GitHub accounts on the same machine using SSH keys and configure Git to use the appropriate user credentials for each repository. This setup will ensure that you can seamlessly switch between accounts and manage your repositories effectively.

Stay Connected for More Tech Insights!

Thanks for reading this guide on managing multiple Git accounts with SSH keys. I hope you found it helpful in streamlining your development workflow. But wait, there's more where that came from!

🐦 Follow me on Twitter: Want quick tips, tech news, and updates on my latest projects? Follow me @appyzdl5 for daily doses of dev goodness.

πŸ“§ Subscribe to my Substack: For in-depth articles, tutorials, and exclusive content, don't forget to subscribe to my Substack newsletter. It's where I share extended versions of my blog posts, along with insights and stories that don't make it to the blog.

Top comments (0)