DEV Community

Ravi Kyada
Ravi Kyada

Posted on • Originally published at towardsaws.com on

The Smart Way to Manage Multiple GitHub Accounts in Linux CLI

If you work with multiple GitHub accounts, such as personal and work accounts, you may face challenges when pushing code to different repositories.

Managing multiple GitHub accounts in Linux can be tricky, but with the right setup, it becomes seamless.

Whether you’re juggling a personal and a work account, or contributing to different organizations, this guide will walk you through setting up and switching between multiple GitHub accounts effortlessly using SSH keys.

Challenges & Common Issues

Before diving into the solution, let’s explore the common challenges:

  • Authentication conflicts : You might be logged in with your personal account but need to push to a work repository.
  • Wrong identity in commits : If Git uses the wrong email and username, your commits may not be correctly attributed.
  • Permission errors : Cloning a repo but realizing you lack the correct SSH key permissions.

Luckily, all these issues can be resolved by setting up multiple SSH keys and configuring Git correctly.

Step 1: Generating SSH Keys

For each GitHub account, you need a separate SSH key. Run the following commands:

ssh-keygen -t ed25519 -C "your_personal_email@example.com" -f ~/.ssh/id_ed25519_personal
ssh-keygen -t ed25519 -C "your_work_email@example.com" -f ~/.ssh/id_ed25519_work
Enter fullscreen mode Exit fullscreen mode

This will create:

  • ~/.ssh/id_ed25519_personal (Private key)
  • ~/.ssh/id_ed25519_personal.pub (Public key)
  • ~/.ssh/id_ed25519_work (Private key)
  • ~/.ssh/id_ed25519_work.pub (Public key)

Step 2: Adding SSH Keys to GitHub

Next, copy and add the public keys to GitHub:

cat ~/.ssh/id_ed25519_personal.pub
cat ~/.ssh/id_ed25519_work.pub
Enter fullscreen mode Exit fullscreen mode

Go to GitHub → Settings → SSH and GPG keys and add the corresponding keys to each account.

Step 3: Configuring SSH

Modify your SSH config file:

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

Add the following:

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

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

This ensures Git uses the correct SSH key for each account.

Step 4: Testing SSH Connections

Verify that each key is correctly associated:

ssh -T git@github.com-personal
ssh -T git@github.com-work
Enter fullscreen mode Exit fullscreen mode

If successful, you’ll see:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Enter fullscreen mode Exit fullscreen mode

Step 5: Cloning Repositories Using Different Accounts

When cloning repositories, use:

  • Personal account:
git clone git@github.com-personal:your_username/repo.git
Enter fullscreen mode Exit fullscreen mode
  • Work account:
git clone git@github.com-work:your_work_username/repo.git
Enter fullscreen mode Exit fullscreen mode

Step 6: Configuring Git Identity Per Repository

Set user details inside the repository:

cd repo_name
git config user.name "Your Name"
git config user.email "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

For global settings:

nano ~/.gitconfig

[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig-work
[includeIf "gitdir:~/personal/"]
    path = ~/.gitconfig-personal
Enter fullscreen mode Exit fullscreen mode

Step 7: Managing SSH Keys Efficiently

Use SSH-Agent to manage keys:

ssh-add ~/.ssh/id_ed25519_personal
ssh-add ~/.ssh/id_ed25519_work
Enter fullscreen mode Exit fullscreen mode

To list added keys:

ssh-add -l
Enter fullscreen mode Exit fullscreen mode

Alternative Method: HTTPS Credential Helper

Instead of SSH, use HTTPS and personal access tokens:

git config --global credential.helper cache
git clone https://github.com/your_username/repo.git
Enter fullscreen mode Exit fullscreen mode

GitHub will prompt you for credentials, and you can use a personal access token instead of a password.

Automating with Scripts

Create a script to switch accounts:

echo "Switching to Work Account"
ssh-add ~/.ssh/id_ed25519_work
Enter fullscreen mode Exit fullscreen mode

Troubleshooting Common Issues

  • SSH key not being used? Use ssh -vT git@github.com-personal to debug.
  • Permission denied? Ensure the SSH key is added to GitHub.

Best Practices

  • Keep SSH keys secure.
  • Use meaningful SSH hostnames.
  • Use HTTPS for occasional access instead of adding too many SSH keys.

Conclusion

Managing multiple GitHub accounts in Linux becomes easy with SSH keys and proper configuration. By following this guide, you can avoid authentication issues and switch between accounts seamlessly.

FAQ

1. Can I use SSH and HTTPS for different accounts?

Yes! You can use SSH for one account and HTTPS for another.

2. How do I remove an SSH key from GitHub?

Go to Settings → SSH and GPG keys , and remove the unwanted key.

3. What if I accidentally commit with the wrong account?

Use:

git commit --amend --author="Correct Name <correct@email.com>"
Enter fullscreen mode Exit fullscreen mode

4. Can I use the same SSH key for multiple GitHub accounts?

No, each GitHub account requires a unique SSH key.

5. How can I see which SSH key Git is using?

Run:

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

By following these steps, you can efficiently manage multiple GitHub accounts on Linux! 🚀

Thank you so much for reading the article till the end! 🙌🏻 Your time and interest truly mean a lot. 😁📃

If you have any questions or thoughts about this blog, feel free to connect with me:

🔗 LinkedIn: Ravi Kyada

🐦 Twitter: @ravijkyada

Until next time, ✌🏻 Cheers to more learning and discovery! 🇮🇳 🚀


Dev Diairies image

User Feedback & The Pivot That Saved The Project

🔥 Check out Episode 3 of Dev Diairies, following a successful Hackathon project turned startup.

Watch full video 🎥

Top comments (0)

DevCycle image

Ship Faster, Stay Flexible.

DevCycle is the first feature flag platform with OpenFeature built-in to every open source SDK, designed to help developers ship faster while avoiding vendor-lock in.

Start shipping

👋 Kindness is contagious

Explore this compelling article, highly praised by the collaborative DEV Community. All developers, whether just starting out or already experienced, are invited to share insights and grow our collective expertise.

A quick “thank you” can lift someone’s spirits—drop your kudos in the comments!

On DEV, sharing experiences sparks innovation and strengthens our connections. If this post resonated with you, a brief note of appreciation goes a long way.

Get Started