Follow

Follow
How to Set Up Multiple Git Accounts on the Same Machine Using SSH Keys.

How to Set Up Multiple Git Accounts on the Same Machine Using SSH Keys.

Ashish maurya's photo
Ashish maurya
ยทFeb 28, 2023ยท

4 min read

Play this article

Table of contents

  • The Problem ๐Ÿ˜’
  • Solution ๐Ÿ‘Œ
  • Setting Up the SSH keys
  • Adding more Accounts and Managing them.

Hello everyone! I'm Ashish Maurya, a full-time software developer and part-time freelancer. With the different projects I work on for different clients, I often have to set up work emails with Git. Unfortunately, it can be tricky to set up multiple Git accounts on the same machine. In this article, I'll show you how to use multiple Git accounts on the same machine using SSH keys.

The Problem ๐Ÿ˜’

Well setting up one git account is easy setting up multiple accounts is not so much especially when you have to keep switching between the different git accounts. (As I mentioned part-time freelance developer)

Solution ๐Ÿ‘Œ

There is a way to set up multiple accounts in Git on the same machine using ssh keys. This article will show how to set up numerous git accounts on the same Machine.

Setting Up the SSH keys

Generating SSH keys.

  • To generate SSH keys just open your favorite terminal or you can open Git bash and write the following command.
$ ssh-keygen -t ed25519 -C "your_email@example.com"
  • Once that is done, you will be prompted to enter a file name. You can name it anything but in case you already have .ssh keys, don't name it the same or else it will be overwritten.

For this article, I will use test it as a dummy username but you can name it anything you like. In case you want to check for existing keys follow this Link.

  • After this, you will be asked to enter a passphrase. You can read more about the passphrase here.

With SSH keys, if someone gains access to your computer, the attacker can gain access to every system that uses that key. To add an extra layer of security, you can add a passphrase to your SSH key. To avoid entering the passphrase every time you connect, you can securely save your passphrase in the SSH agent.

  • You will see a key fingerprint will be generated.

Adding SSH key

  • Once you are done with the above steps you will be needing to add the SSH key. For that, you can run the following command.
$ eval "$(ssh-agent -s)"
>> Agent pid 375
  • Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace the test in the command with the name of your private key file.
$ ssh-add ~/.ssh/test
Identity added: /c/Users/Lenovo/.ssh/test (your_email@example.com)

Adding the above SSH key to GitHub

  • Copy the above SSH public key to your clipboard.

      $ clip < ~/.ssh/test.pub
        # Copies the contents of the test.pub file to your clipboard
    
  • Open the setting in your GitHub account.

  • Open the SSH key & GPG keys from the sidebar.

  • Click on add New Key and a prompt will open in that text area paste the keys and Add them.

  • Once that is added you have added the key Successfully.

Adding more Accounts and Managing them.

Adding Account

Now repeat the same process for your work account or personal account. And once done.

Maintaining multiple Accounts

  1. Edit/Create ssh config file (~/.ssh/config):

     # Default github account: personal
     Host github.com
        HostName github.com
        IdentityFile ~/.ssh/personal # your personal SSH key
        IdentitiesOnly yes
    
     # Other github account: test
     Host github-test
        HostName github.com
        IdentityFile ~/.ssh/test #Your test/ Organization key
        IdentitiesOnly yes
    
  2. Adding SSH private key to your agent.

     $ ssh-add ~/.ssh/personal
     $ ssh-add ~/.ssh/test
    
  3. Test the connection.

     $ ssh -T git@github.com
     $ ssh -T git@github-test
    

    you will get the following prompt with each command, you might see this kind of warning type yes in each case.

     The authenticity of host 'github.com (192.30.252.1)' can't be established.
     RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:
     Are you sure you want to continue connecting (yes/no)?
    
  4. If everything is OK, you will see these messages:

     Hi personal! You've successfully authenticated, but GitHub does not provide shell access.
    
     Hi test! You've successfully authenticated, but GitHub does not provide shell access.
    
  5. Now all are set, just clone your repositories

     $ git clone git@github-test:org2/project2.git /path/to/project2
     $ cd /path/to/project2
     $ git config user.email "test@org2.com"
     $ git config user.name  "Test"
    

Conclusion

In conclusion, setting up multiple Git accounts on the same machine can be tricky, but it is possible with the use of SSH keys. By following the steps outlined in this article, you can easily manage multiple Git accounts from the same machine. With SSH keys, you can securely access multiple accounts without having to constantly enter and store passwords.

References

github.com
https://gist.github.com/oanhnn/80a89405ab9023894df7
https://www.freecodecamp.org/news/manage-multiple-github-accounts-the-ssh-way-2dadc30ccaca/

ย 
Share this