Skip to main content

How to Manage Multiple Git Accounts on One Machine Like a Pro

Learn how to manage multiple Git accounts (personal, work, client) on a single machine efficiently. Real-world strategies from 8+ years of web development.

We earn commissions when you shop through the links below.
Shafat Mahmud Khan
11 min read
How to Manage Multiple Git Accounts on One Machine Like a Pro

As a web developer with over eight years in the trenches, building everything from WordPress plugins like OpenWA WhatsApp Gateway to full-blown Laravel-based School ERP systems, I’ve faced my fair share of “developer pains.” One common headache that many of us encounter early on—and sometimes still grapple with—is how to manage multiple Git accounts on one machine. It’s a scenario that hits hard when you’re juggling a personal GitHub for open-source contributions or side projects like my Frontend File Explorer plugin, alongside work repositories on GitLab or client projects on Bitbucket.

I’ve been there, pulling my hair out trying to figure out why my commit history showed my work email on a personal project, or vice versa. It’s more than just an aesthetic issue; it’s about maintaining professional boundaries, ensuring proper attribution, and avoiding security pitfalls. This isn’t theory; it’s the exact setup I’ve refined over years of building diverse projects and deploying them, ensuring I always commit with the correct identity.

Understanding the Core Problem: Why We Need Multiple Git Accounts

The default Git setup assumes you have one global identity: git config --global user.name and git config --global user.email. This is fine for someone who only ever contributes to one type of repository under a single identity. But for developers like us, that’s rarely the case. Consider these common scenarios:

  • Personal Projects vs. Work Projects: You want your personal GitHub profile to showcase your contributions to projects like OpenWA WhatsApp Gateway, but your employer’s GitLab expects commits from your work email.
  • Client Projects: Each client might have their own Git hosting (e.g., Bitbucket) and require you to use an email associated with their organization for their specific repositories.
  • Different GitHub/GitLab Organizations: Even within the same platform, you might have separate personal and organizational accounts, each demanding a distinct identity for pushing code.

In my work on the School ERP system, for example, the client had a dedicated GitLab instance where all development took place. My commits needed to reflect my identity within their organization. Simultaneously, I was pushing updates to my personal Frontend File Explorer plugin on GitHub. Trying to manage this without a robust system led to constant global config changes, forgotten settings, and incorrect commit authors. It’s a mess, and it drains productivity.

The Power of SSH Keys for Git Authentication

Before we dive into the “how,” let’s talk about the “what.” The most elegant and secure way to manage multiple Git accounts on one machine relies heavily on SSH (Secure Shell) keys. Forget constantly typing usernames and passwords (or even personal access tokens for HTTPS) for every push. SSH keys provide a cryptographic way to authenticate yourself to a remote server without exposing your credentials.

An SSH key pair consists of two parts: a private key, which you keep secret on your local machine, and a public key, which you upload to your Git hosting service (GitHub, GitLab, Bitbucket). When you try to interact with a remote repository, your local SSH client presents the public key to the server. If the server recognizes it, it challenges your client to prove you possess the corresponding private key. This handshake happens automatically and securely, granting you access.

Step-by-Step Guide: Setting Up Multiple SSH Keys

The foundation of effectively managing multiple Git accounts on one machine is setting up unique SSH keys for each identity.

Generating SSH Keys for Each Account

You’ll need to generate a distinct SSH key pair for each Git identity you wish to manage. I recommend using descriptive filenames to avoid confusion.

Open your terminal and run the following commands, replacing the email and filename with your specific details:

ssh-keygen -t rsa -b 4096 -C "personal@example.com" -f ~/.ssh/id_rsa_personal_github
ssh-keygen -t rsa -b 4096 -C "work@example.com" -f ~/.ssh/id_rsa_work_github
ssh-keygen -t rsa -b 4096 -C "client@example.com" -f ~/.ssh/id_rsa_client_gitlab

When prompted, you can choose to add a passphrase for extra security. I usually do, especially for keys linked to sensitive client projects or my main work accounts. For my OpenWA WhatsApp Gateway plugin, which is publicly accessible, I might opt for no passphrase for convenience, but for client-specific repositories, a passphrase is a must.

After generation, you’ll have private keys (e.g., id_rsa_personal_github) and their corresponding public keys (e.g., id_rsa_personal_github.pub) in your ~/.ssh/ directory. You then need to add the public keys to the respective Git hosting services (GitHub, GitLab, Bitbucket) under the correct accounts. Each platform has a section in its settings for SSH keys.

Configuring the SSH Agent

The SSH agent is a background program that holds your private keys in memory, so you don’t have to enter your passphrase every time you use an SSH key. First, ensure the SSH agent is running:

eval "$(ssh-agent -s)"

Then, add your newly generated private keys to the agent. You’ll be prompted for any passphrases you set:

ssh-add ~/.ssh/id_rsa_personal_github
ssh-add ~/.ssh/id_rsa_work_github
ssh-add ~/.ssh/id_rsa_client_gitlab

You can verify which keys are loaded with ssh-add -l. For developers who enjoy automating their workflows, understanding how to configure your environment is crucial, much like learning to automate repetitive tasks with shell scripts. This agent setup is a classic example of automating a repetitive authentication step.

A developer at a desk with multiple monitors, each displaying code or terminal windows related to different Git repositories. One screen shows SSH key configurations, another a project directory, symbolizing the efficient management of diverse development environments.
This setup illustrates how I mentally organize my projects and their corresponding Git identities. Having distinct virtual spaces helps reinforce the SSH config logic.

Creating an SSH Config File (~/.ssh/config)

This is the secret sauce to gracefully how to manage multiple Git accounts on one machine. The ~/.ssh/config file tells your SSH client which private key to use when connecting to a specific host. If you don’t have this file, create it.

# ~/.ssh/config

Host github.com-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_personal_github
    IdentitiesOnly yes

Host github.com-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_work_github
    IdentitiesOnly yes

Host gitlab.com-client-erp
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/id_rsa_client_gitlab
    IdentitiesOnly yes

Host bitbucket.org-pos
    HostName bitbucket.org
    User git
    IdentityFile ~/.ssh/id_rsa_pos_bitbucket
    IdentitiesOnly yes

Let’s break down these directives:

  • Host: This is an alias you define. When Git tries to connect to a repository using this alias (e.g., github.com-personal), SSH will use the configuration defined under it.
  • HostName: The actual domain of the Git hosting service (e.g., github.com, gitlab.com, bitbucket.org).
  • User: Always git for Git remotes.
  • IdentityFile: The path to the private SSH key you want to use for this specific host.
  • IdentitiesOnly yes: This is crucial. It tells SSH to only try the specific IdentityFile for this host, preventing it from offering other keys by default, which can lead to “Too many authentication failures” errors.

With this configuration, when I’m working on my personal projects, I’ll use github.com-personal. If it’s a work-related repository that still happens to be on GitHub, I’d use github.com-work. For my clients, like the School ERP or the Point of Sale system where the codebase lives on GitLab or Bitbucket, I use the aliases like gitlab.com-client-erp or bitbucket.org-pos.

Configuring Git for Specific Projects

SSH keys handle authentication, but Git still needs to know who is making the commit (your name and email). We override the global Git configuration on a per-repository basis.

Local Git Configuration (.git/config)

After cloning a repository, navigate into its directory. Then, set the local user.name and user.email:

cd ~/dev/personal/openwa-whatsapp-gateway
git config user.name "Shafat Mahmud Khan"
git config user.email "shafat@personal.com"

Then, for a work project:

cd ~/dev/work/school-erp
git config user.name "Shafat Khan (Work)"
git config user.email "shafat.work@example.com"

These commands write directly to the .git/config file within that specific repository, overriding any global settings. To verify, run git config --list from within the repository. This ensures that every commit from this repository correctly identifies you with the appropriate name and email, maintaining clear commit histories for all your projects.

Using includeIf for Directory-Based Configuration (Advanced)

If you’re like me and organize your projects into distinct directories (e.g., ~/dev/personal, ~/dev/work, ~/dev/clients), you can automate the local Git config even further using includeIf directives in your global ~/.gitconfig. This feature dynamically applies a different configuration file based on the directory a repository resides in.

First, set your default (e.g., personal) identity in ~/.gitconfig:

# ~/.gitconfig
[user]
    name = Shafat Mahmud Khan
    email = shafat@personal.com

[includeIf "gitdir:~/dev/work/"]
    path = ~/.gitconfig-work

[includeIf "gitdir:~/dev/clients/"]
    path = ~/.gitconfig-clients

Then, create separate configuration files for your work and client directories:

# ~/.gitconfig-work
[user]
    name = Shafat Khan (Work)
    email = shafat.work@example.com

# ~/.gitconfig-clients
[user]
    name = Shafat Khan (Client)
    email = shafat.client@example.com

With this setup, any repository cloned into ~/dev/work/ will automatically use shafat.work@example.com for commits, while those in ~/dev/clients/ will use shafat.client@example.com. Anything outside these directories defaults to your personal details. This became a lifesaver for me, especially when managing multiple client projects for WooCommerce extensions or the Point of Sale system, each with their own dev setup, distinct from my personal projects. This is a robust way to how to manage multiple Git accounts on one machine efficiently.

Testing Your Setup

It’s crucial to test your configuration to ensure everything is working as expected. You can test your SSH connection to your aliased hosts:

ssh -T github.com-personal
ssh -T github.com-work
ssh -T gitlab.com-client-erp

You should see a message indicating successful authentication (e.g., "Hi username! You've successfully authenticated..."). If you see "Permission denied" or similar, something is wrong with your SSH keys or config.

When cloning new repositories, use your defined aliases:

git clone git@github.com-personal:myuser/my-personal-repo.git
git clone git@github.com-work:myorg/my-work-repo.git
git clone git@gitlab.com-client-erp:clientorg/school-erp.git

After cloning, navigate into the repository and verify your Git user configuration:

cd my-personal-repo
git config user.name
git config user.email

The output should reflect the correct name and email for that specific repository, either from your manual local config or from the includeIf magic.

Hosting Recommendations

When you're deploying these various projects, whether it's a new React frontend for a School ERP or a robust WooCommerce store using OpenWA, your hosting choice matters. Just like managing your Git accounts, selecting the right hosting can significantly impact your workflow and project success.

For those just starting out, or managing smaller client sites on a budget, I often recommend Hostinger. They offer a great balance of performance and cost, which is perfect for an initial WordPress site, like a simple landing page, or a small custom application that doesn’t demand extreme resources. My readers can even get 20% off, which is a sweet deal when every penny counts on a new venture.

However, for high-traffic WooCommerce sites or critical client applications where performance and reliability are paramount, especially when I'm dealing with complex setups or needing a managed environment, Kinsta is my go-to. Their premium managed WordPress and application hosting with Google Cloud infrastructure and edge caching just performs beautifully. It’s ideal for client projects where downtime isn’t an option and speed is a key performance indicator.

And for those larger custom applications, APIs, or database servers—perhaps for a scalable School ERP backend, a custom React application, or the Point of Sale system—where I need full control over the server environment and robust scaling options, DigitalOcean droplets are fantastic. They give you the flexibility to deploy almost anything, offering cloud VPS hosting with straightforward pricing, which is invaluable for developers who need to configure every aspect of their infrastructure.

Troubleshooting Common Issues

Even with the best setup, you might encounter a snag or two. Here are a couple of common issues and how I tackle them:

  • "Permission denied (publickey)."

    This is the most frequent error. It means your SSH client couldn’t authenticate with the server using the provided keys. Check the following:

    • Is the correct public key uploaded to your Git hosting service? Double-check the account and the key.
    • Is your SSH agent running and are your keys loaded? Run eval "$(ssh-agent -s)" and ssh-add -l. If a key is missing, add it with ssh-add ~/.ssh/your_key.
    • Are your private key permissions correct? Private keys should only be readable by you. Run chmod 600 ~/.ssh/your_private_key.
    • Is your ~/.ssh/config file correct? Check for typos in HostName, User, and IdentityFile paths.
    • Are you using the correct remote URL? Ensure you’re using the SSH alias you defined (e.g., git@github.com-personal:user/repo.git).

    Debugging these kinds of configuration issues reminds me of the importance of systematic troubleshooting, much like when I had to diagnose a Drupal White Screen of Death after a module update – it's all about methodically checking your setup.

  • Incorrect User/Email in Commits

    If your commits are still showing the wrong author despite your efforts, it’s almost always a local Git config issue. Navigate into the repository and run git config --list. Look specifically at user.name and user.email. If they’re wrong, set

Share

Need help with your project?

Comments(0)

No comments yet. Be the first to share your thoughts.

Leave a comment

Your email is optional and never shown publicly.

All posts
Shafat Mahmud Khan

Shafat Mahmud Khan

WordPress & full-stack dev with 8+ years. Built OpenWA, File Explorer, ERP systems

GitHub
Recommended Hosting

Launch with Hostinger & Save 20%

Fast, reliable hosting for WordPress, Next.js, and more. Trusted by millions.

Free SSL24/7 Support1-Click WP Install99.9% Uptime
20% OFFexclusive discount
Get 20% Off

Affiliate disclosure: I earn a commission at no extra cost to you.

Latest Articles