Git Guru: Juggling Multiple Accounts Like a Pro | Chandrashekhar Kachawa | Tech Blog

Git Guru: Juggling Multiple Accounts Like a Pro

git

ssh

Ever feel like your code is having an identity crisis? You’re working on a personal project for your own GitHub, but then you switch over to your work stuff for the company’s GitLab. It’s a pain, right? Your commits get all mixed up, and you’re never sure which email is attached to what. You’ve thought about using different computers, but who has time for that? And all those workarounds? They just feel messy.

Well, guess what? There’s a super simple, clean solution, and it’s all thanks to a tiny file called ~/.ssh/config. This little file is like a secret key that tells your computer exactly which identity to use for each project. No more mix-ups, no more headaches.

The Solution: The Magic of ~/.ssh/config

Think of the ~/.ssh/config file as your personal cheat sheet for Git. You can give a custom nickname to each of your accounts and tell Git which special key to use for each one. That way, you don’t have to manually switch anything!

Ready to get started? Let’s walk through the steps.

Step 1: Get a New SSH Key

First things first, you’ll need a new key just for your second account. Open up your terminal and type this command. It’ll create a new, separate key so you don’t mess up your old one.

# Generate a key for your work account
$ ssh-keygen -t ed25519 -C "work_email@example.com" -f ~/.ssh/id_ed25519_work

The command will ask if you want a password for the key. It’s a good idea to add one for extra security! After that, you’ll have two new files: your secret key (id_ed25519_work) and your public key (id_ed25519_work.pub).

Step 2: Add Your New Key to Your Account

Now, take that new public key and add it to your second Git account (like your work’s GitHub or GitLab).

To see the public key, run this:

$ cat ~/.ssh/id_ed25519_work.pub

Copy everything you see there.

  1. Log in to your Git account.
  2. Find Settings -> SSH and GPG keys.
  3. Click New SSH key, give it a name like “My Laptop - Work,” and paste the key you copied.

Step 3: Edit Your SSH Config File

This is where you make it all work together. Head to your ~/.ssh folder and either open or create the config file.

$ touch ~/.ssh/config # Only if it doesn't exist
$ open ~/.ssh/config  # Or use your favorite editor

Inside, you’ll create a little block of text for each of your accounts. It’ll look something like this:

# Personal GitHub Account
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519
  AddKeysToAgent yes

# Work GitHub Account
Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work
  AddKeysToAgent yes

See that “work” part? That’s your new nickname! The HostName is still the real website, but when you use github.com-work as the Host, your computer knows to use the special key you just made.

Step 4: Clone the Repository with Your New Nickname

Now, when you want to clone a work project, you don’t use the normal URL. You just swap the github.com part with your new Host nickname.

Standard URL: git@github.com:your-organization/your-work-project.git

Your New URL: git@github.com-work:your-organization/your-work-project.git

That’s it! When you run git clone or git push with this new URL, Git will automatically use the correct key and keep everything nice and separate.

Step 5: Set Your Author Info

This last step is super important. You should also tell each repository what your name and email are. That way, your commits are always credited to the right person, not your global Git settings.

To do this, just go into your project folder and run these two commands:

$ git config user.name "Your Work Name"
$ git config user.email "work_email@example.com"

This sets the author info for just this one project, and it won’t affect any others.

Bonus: Create Quick Aliases for Author Info

Want to make this even faster? You can create Git aliases to switch your author info with a single command. Just add these aliases to your global Git configuration.

Add the aliases: Run these two commands in your terminal:

git config --global alias.work '!git config user.name "Your Work Name" && git config user.email "work_email@example.com"'
git config --global alias.personal '!git config user.name "Your Personal Name" && git config user.email "personal_email@example.com"'

Switch identities: Now, when you’re in a project folder, just type the alias to set the author info.

# To set the author info to your work identity
$ git work

# To set the author info to your personal identity
$ git personal

You’re all set! Now you’re a Git pro, and your code will thank you for it.

Latest Posts

Enjoyed this article? Follow me on X for more content and updates!

Follow @Ctrixdev