DEV Community

Deepika Gunda
Deepika Gunda

Posted on

Managing Multiple Git Identities on Windows for Different Projects

If you're a developer managing both personal and work projects, you might find yourself needing to juggle multiple Git accounts. Keeping credentials and SSH keys separate for different projects can be a bit challenging, especially if you want everything under C:\work\personal to use one identity and everything else to use another.

This guide walks you through setting up Git so it automatically switches credentials based on the project directory. You’ll only need to set this up once, and Git will handle the rest!


Step 1: Setting Up Global Git Configuration

First, let’s set up a global Git configuration. This will act as the default identity for any repositories outside your specified folder.

  1. Open a Command Prompt or PowerShell window.
  2. Set up your global credentials:
   git config --global user.name "Your Global Username"
   git config --global user.email "yourglobal@example.com"
Enter fullscreen mode Exit fullscreen mode

Now, Git will use these settings for all repositories except where we override them.


Step 2: Creating a Custom Git Config for C:\work\personal

To apply a different Git identity to all repositories within C:\work\personal, we’ll create a separate configuration file with conditional includes.

  1. Open your global .gitconfig file, usually at:
   C:\Users\YourUsername\.gitconfig
Enter fullscreen mode Exit fullscreen mode
  1. Add a conditional include at the end of this file to specify a different config file for C:\work\personal.
   [includeIf "gitdir/i:C:/work/personal/"]
       path = C:/Users/YourUsername/.gitconfig-personal
Enter fullscreen mode Exit fullscreen mode
  1. Save and close the .gitconfig file.

  2. Now, create the custom config file, C:\Users\YourUsername\.gitconfig-personal, and add the identity details for your personal projects:

   [user]
       name = "Your Personal Username"
       email = "personal@example.com"
Enter fullscreen mode Exit fullscreen mode

Git will automatically apply these settings for any repository under C:\work\personal.


Step 3: Configuring SSH Keys for Multiple Accounts

If these accounts also require separate SSH keys, here’s how to set that up:

  1. Generate SSH keys if you don’t already have them:
   ssh-keygen -t rsa -b 4096 -C "yourglobal@example.com"
Enter fullscreen mode Exit fullscreen mode
  • Save this key as id_rsa. Repeat the process for your personal account, saving as id_rsa_personal.
  1. Add SSH keys to the SSH agent:
   ssh-agent -s
   ssh-add C:\Users\YourUsername\.ssh\id_rsa
   ssh-add C:\Users\YourUsername\.ssh\id_rsa_personal
Enter fullscreen mode Exit fullscreen mode
  1. Configure the SSH config file to tell Git which key to use for each account.

Open or create config in your .ssh directory:

   notepad C:\Users\YourUsername\.ssh\config
Enter fullscreen mode Exit fullscreen mode

Then add entries like these:

   # Default account
   Host github.com
       HostName github.com
       User git
       IdentityFile C:/Users/YourUsername/.ssh/id_rsa

   # Personal account for C:\work\personal
   Host personal.github.com
       HostName github.com
       User git
       IdentityFile C:/Users/YourUsername/.ssh/id_rsa_personal
Enter fullscreen mode Exit fullscreen mode
  1. Update Git Remote URLs in repositories under C:\work\personal to use personal.github.com:
   cd C:\work\personal\some-repo
   git remote set-url origin git@personal.github.com:username/repository.git
Enter fullscreen mode Exit fullscreen mode

Step 4: Testing Your Setup

To confirm everything is working as expected:

  1. Check that repositories under C:\work\personal are using the right user details:
   cd C:\work\personal\some-repo
   git config user.name
   git config user.email
Enter fullscreen mode Exit fullscreen mode
  1. Test your SSH connection:
   ssh -T git@personal.github.com  # Should authenticate with your personal SSH key
   ssh -T git@github.com           # Should authenticate with your global SSH key
Enter fullscreen mode Exit fullscreen mode

With this setup, Git will automatically apply the correct identity and SSH key for each account based on the repository location. No more manual switching between credentials—just a smoother workflow all around!


Let me know if you have any questions, or share your tips on managing multiple Git identities in the comments! Happy coding!

Top comments (0)