Manage git accounts in no time

Daniel Stefan
4 min readMar 17, 2022
Photo by Roman Synkevych on Unsplash

Since I’ve been working in the freelancing industry, most of the time, I had to create new Github accounts based on contracted company e-mails. Also, there might be scenarios when I would like to work on my Github profile or just have fun on my personal account.

Every time I had to run lots of commands to switch the global user and email and password and credential username and so on… This was cumbersome… 😒

What has been working for me

To make the experience smooth, let’s create a new ssh key:

ssh-keygen -f ~/.ssh/id_rsa_company -t rsa -C “your-email-address”

Let’s break down this command a bit:

  • -f represents the file we want to create
  • -t represents the type
  • rsa is the type of encryption we are using
  • -C comes from comment

As a personal preference, I would like to differentiate them based on company name or utility. So if you would have a more generic approach, maybe you can split your keys into id_rsa_personal and id_rsa_business (or just let your already generated id_rsa as a default key).

Now you will be prompted to add a passphrase twice:

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

You can add one or just leave both prompts empty and just press Enter.

If everything works as expected, you should have a console output similar to this one:

Your identification has been saved in id_rsa_companyYour public key has been saved in id_rsa_company.pubThe key fingerprint is:SHA256:..... “your-email-address”The key's randomart image is:+---[RSA 3333]----+|AAA+.            |........
........
+----[SHA256]-----+

Now let’s double-check this and let’s run in our terminal ls -l .

This will help us check if our newly created files are in the right place. The output should look similar to this one:

....................-rw-------  1 user  group  2622 Date id_rsa_company-rw-r--r--  1 user  group   585 Date id_rsa_company.pub

Of cours you might find there all your previously generated files, like old ssh keys, knownhost or a ssh config file.

Make sure you have user read and write permissions for your newly generated private key. (just check permissions from the previous list command)

If not you can run:

chmod 600 ~/.ssh/id_rsa_company

The next thing we have to do is run the following command to copy to clipboard your public company key:

pbcopy < ~/.ssh/id_rsa_company.pub

Now let’s go into our Github account, and click on Settings ->SSH and GPG keys -> New SSH key.

Add your preferred name, maybe something like COMPANY Mac Key. I would prefer to name it based on utility [company name, personal, business], location [Personal-machine, work-machine, operating systems], and hit Add SSH key.

We are almost there. We have a few more steps and this will be over soon 😄.

We need a config file in our .ssh folder to manage everything for us, so let’s create it:

nano ~/.ssh/config

In this file, add the following lines:

# My personal github settings / Default
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/your_default_id_rsa

This is your default Github account that is going to use your default id_rsa key. What this actually does, is creating an alias-like host, and whenever your git remote will be git@github.com:… the default ssh file will be passed.

And also copy the following lines down below the previous ones:

# Company X github settings
Host github-companyName
HostName github.com
User git
IdentityFile ~/.ssh/your_id_rsa_company

We do almost the same thing, but we are going to change the alias into our desired naming convention.

Of cours if you already have a ssh config file, just add the lines above at the bottom of the page.

So that being said, the final step would be to go into your existing git repository and run your commands accordingly.

If the repository is already created and is a personal repository, just run:

git remote set-url origin git@github.com:my-github/test.git

If you are creating a new personal repository, just change the git command with:

git remote add origin git@github.com:my-github/test.git

If you would like to use the company ssh key newly added, then just run:

git remote set-url origin git@github-companyName:my-github/test.git

Or if it is a new company repo just add it:

git remote add origin git@github-companyName:my-github/test.git

Please notice the difference between git@github.com and git@github-companyName . This will explicitly tell our ssh config file created earlier what IdentityFile to use (what ssh file to use, in our case id_rsa_company or id_rsa_default).

I know this was a bit long, but with this setup, you will feel like you have a universal git account ready for everything.

An important note is that every time you would want to add a new ssh config, for a different company, or maybe change your current setup, you will have to change your ~/.ssh/config file to reflect your desired behavior.

Test our integration by pushing or pulling changes from specific remotes, and Voilla 🎊!! Everything works as expected now 🚀!

Hope this solution helped!

--

--

Daniel Stefan

Developer 💻 | Writer 📘 | Passionate about JavaScript & Typescript. Currently doing my best to make the world a better place!