DEV Community

SebasQuiroga
SebasQuiroga

Posted on

Managing Multiple GitHub Accounts (Without UI Interaction 😎)

If you work with both personal and work-related projects hosted on GitHub, and you’re using the same machine to work on them, configuring Git to push to the correct GitHub account can be tricky. In this article, we’ll go through the process of setting up your SSH keys, configuring Git settings, and ensuring your repositories use the correct account for each git push

Step 1: Generate SSH Keys for each GitHub account
GitHub uses SSH keys to authenticate you. To work with multiple GitHub accounts, you’ll need to create separate SSH keys for each account.

Commands to generate SSH keys:
Open your terminal and run the following command to generate a new SSH key for your personal GitHub account:

ssh-keygen -t ed25519 -C "your-email@example.com"
Enter fullscreen mode Exit fullscreen mode

When prompted for a file to save the key, specify a unique name, for example:

For personal GitHub account: [recommended_path]/id_ed25519_personal
For work GitHub account: [recommended_path]/id_ed25519_work

Repeat the process for your work GitHub account, but with a different file name:

ssh-keygen -t ed25519 -C "your-work-email@example.com"
Enter fullscreen mode Exit fullscreen mode

Now, instead of manually copying the SSH public key to GitHub's UI, you can automatically copy it to the clipboard using pbcopy command:

For Personal GitHub account, run:

pbcopy < ~/.ssh/id_ed25519_personal.pub
Enter fullscreen mode Exit fullscreen mode

This command copies the contents of your public key directly to the clipboard.

After running this command, you can simply add the public key to GitHub:

Go to GitHub.com > Avatar > Settings > SSH and GPG Keys.

Click New SSH Key and paste the copied key (it’s already in your clipboard).

Step 2: Configure SSH to use the correct keys

Now that you have SSH keys, you need to tell your Mac which key to use for each GitHub account. This is done via the ~/.ssh/config file.

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

Add the following configurations for both GitHub accounts:

# Personal GitHub Account
Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_personal

# Work GitHub Account
Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work
Enter fullscreen mode Exit fullscreen mode

These settings tell your machine to use the corresponding SSH key when you interact with GitHub based on the host name (github-personal or github-work).

Step 3: Configure Git to Use the Correct Remote URL

Instead of manually updating the remote URL by commands every time you clone a repository, we'll set this up directly in the .git/config file. This ensures you’re always using the right GitHub account for your repos.

In each of your repositories, you’ll need to update the .git/config file to point to the correct SSH host and remote URL.

Open your repository folder.
Open the .git/config file on your project folder:

nano .git/config
Enter fullscreen mode Exit fullscreen mode

Add/update the [remote "origin"] section to use the correct SSH URL based on your GitHub account:

For personal projects, update the url to:

[remote "origin"]
    url = git@github-personal:your-username-from-repo/your-repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

[user]
    name = "Your name"
    email = "your-email@example.com"
Enter fullscreen mode Exit fullscreen mode

This ensures the repository uses the correct GitHub account for pushing and pulling code.

Step 5: Verify everything is set up correctly

To ensure that everything is working as expected, you can verify the configuration with the following checks:

To verify that your SSH keys are configured correctly, test the connection with GitHub:

For your personal GitHub account, run:

ssh -T git@github-personal
Enter fullscreen mode Exit fullscreen mode

Step 6: Push code to GitHub

Now that everything is set up, you can push your code to the appropriate GitHub account based on the repository configuration.

For personal projects:

git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Git will automatically use the correct SSH key and GitHub account, as specified in your .git/config.

Happy coding! 🚀

Top comments (0)