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.
- Open a Command Prompt or PowerShell window.
- Set up your global credentials:
git config --global user.name "Your Global Username"
git config --global user.email "yourglobal@example.com"
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.
- Open your global
.gitconfig
file, usually at:
C:\Users\YourUsername\.gitconfig
- 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
Save and close the
.gitconfig
file.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"
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:
- Generate SSH keys if you don’t already have them:
ssh-keygen -t rsa -b 4096 -C "yourglobal@example.com"
- Save this key as
id_rsa
. Repeat the process for your personal account, saving asid_rsa_personal
.
- 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
- 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
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
-
Update Git Remote URLs in repositories under
C:\work\personal
to usepersonal.github.com
:
cd C:\work\personal\some-repo
git remote set-url origin git@personal.github.com:username/repository.git
Step 4: Testing Your Setup
To confirm everything is working as expected:
- 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
- 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
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)