DEV Community

Cover image for Managing Multiple Git Identities on Windows for Different Projects
Deepika Gunda
Deepika Gunda

Posted on • Edited on

Managing Multiple Git Identities on Windows for Different Projects

Are you juggling multiple Git accounts for work, personal projects, or freelancing gigs? Managing multiple identities on Git can be a hassle, but with the right setup, Windows can handle it seamlessly. Imagine switching from work to personal projects without ever having to manually update your Git or SSH credentials again! Let’s dive in and set up Git to automatically choose the right identity based on the folder.

This guide will cover:

  • Configuring Git with different identities based on folder paths
  • Setting up SSH for automatic key selection
  • Enabling ssh-agent to auto-load on Windows startup
  • Basic Git operations and confirming your setup with git config

So let’s get started.


Step 1: Configure Git for Multiple Identities

The first step is to make Git aware of different identities based on the project folder.

  1. Open your global Git configuration file (.gitconfig), located at C:\Users\YourUsername\.gitconfig.
  2. Add the following lines to define a default work identity and set a conditional include for personal projects:
   [user]
       name = "Your Work Username"
       email = "work@example.com"

   [includeIf "gitdir/i:C:/work/personal/"]
       path = C:/Users/YourUsername/.gitconfig-personal
Enter fullscreen mode Exit fullscreen mode
  1. Create a new Git configuration file at C:\Users\YourUsername\.gitconfig-personal for personal projects:
   [user]
       name = "Your Personal Username"
       email = "personal@example.com"
Enter fullscreen mode Exit fullscreen mode

Now, Git will use the global identity for all repositories except those under C:\work\personal, where it will automatically use your personal credentials.


Step 2: Set Up SSH for Automatic Key Selection

To keep things seamless, let’s set up SSH to use different keys based on the Git account without needing to change each repository’s remote URL.

  1. Open or create your SSH config file at C:\Users\YourUsername\.ssh\config.
  2. Add the following entries to specify which SSH key to use based on the Git host:
   # Default GitHub account (for work or general use)
   Host github.com
       HostName github.com
       User git
       IdentityFile C:/Users/YourUsername/.ssh/id_rsa  # Work SSH key path

   # Alias for personal GitHub account
   Host github-personal
       HostName github.com
       User git
       IdentityFile C:/Users/YourUsername/.ssh/id_rsa_personal  # Personal SSH key path
Enter fullscreen mode Exit fullscreen mode

This SSH config setup allows Git to automatically select the correct key when accessing each account. No need to modify each repository's remote URL manually!


Step 3: Enable ssh-agent to Auto-Load on Startup

To save yourself from repeatedly adding SSH keys, let’s configure ssh-agent to automatically load your keys each time you log in.

  1. Enable the SSH agent service to start on boot:

    • Open PowerShell as an Administrator and run:
     Get-Service ssh-agent | Set-Service -StartupType Automatic
     Start-Service ssh-agent
    
  2. Automatically add keys on login:

    Setting Up a Scheduled Task to Run ssh-add on Login

  3. Open Task Scheduler:

    • Open Task Scheduler from the Start menu or press Win + R, type taskschd.msc, and press Enter.
  4. Create a New Task:

    • In Task Scheduler, select Action > Create Task.
  5. Configure the Task:

    • Under the General tab, name the task (e.g., “Add SSH Key to Agent”).
    • Check Run only when user is logged on and Run with highest privileges.
  6. Trigger the Task:

    • Go to the Triggers tab and click New.
    • Set the trigger to Begin the task: At log on and ensure it’s set for Any user.
  7. Add Action to Run ssh-add:

    • Go to the Actions tab and click New.
    • Set Action to Start a Program.
    • For the Program/script field, enter the path to ssh-add.
     C:\Windows\System32\OpenSSH\ssh-add.exe
    
  • In the Add arguments field, specify the paths to your SSH private keys:

     -Command "ssh-add C:/Users/YourUsername/.ssh/id_rsa; ssh-add C:/Users/YourUsername/.ssh/id_rsa_personal"
    
  1. Save the Task:
    • Click OK to save the task.

Now, every time you start Windows, ssh-agent will be up and running with your keys loaded.


Step 4: Testing the Setup with Basic Git Operations

Let’s test the setup and see how Git will behave differently depending on your folder location.

Example Git Operations

In the terminal, try the following commands to confirm everything is working as expected:

  1. Navigate to your work project (C:\work\project):
   git config user.name  # Should show your work username
   git config user.email  # Should show your work email
Enter fullscreen mode Exit fullscreen mode
  1. Navigate to a personal project (C:\work\personal\my-project):
   git config user.name  # Should show your personal username
   git config user.email  # Should show your personal email
Enter fullscreen mode Exit fullscreen mode
  1. Run Git commands:
   git status  # Check the current branch and changes
   git commit -m "A sample commit"  # Confirm your identity in commit logs
Enter fullscreen mode Exit fullscreen mode

Step 5: Confirming the Git Configuration with git config --list

To get a full list of the active configuration in each context, use git config --list --show-origin:

  • This command will display all configurations, including which file (global, included, or local) each setting comes from.
  • It’s a useful way to confirm that your setup is working as expected in each directory.

For example, run this in C:\work\personal\my-project to confirm the personal configuration is loaded, then try it again in C:\work\project to see the work configuration.


Wrapping Up

With this setup, you’re ready to manage multiple Git identities on Windows without constantly switching configurations or updating SSH keys manually. By setting up Git and SSH configs this way, you’ve made it easy to separate work and personal projects with minimal hassle.

Happy coding, and may your Git journeys be smooth and productive! Let me know in the comments if you have questions or any cool tips on managing multiple identities in Git.

Top comments (0)