7 mins | Dec 30, 2024
Managing multiple GitLab accounts on a single computer can be challenging, especially if you want to keep work and personal projects separate. Here’s a step-by-step guide on how to set up two accounts on a Windows device, using SSH keys for each. I’ll also cover some common errors and troubleshooting tips.
You’ll need two SSH keys: one for your personal account and one for your work account.
1. Open Git Bash on Windows.
2. Generate a key for your personal account:
ssh-keygen -t rsa -C "your-personal-email@example.com" -f C:\Users\tantr\.ssh\id_rsa_personal
This command creates an SSH key pair (private and public) for your personal GitLab account. Leave the passphrase blank if you don’t need extra security
3. Generate a key for your work account:
ssh-keygen -t rsa -C "your-work-email@example.com" -f C:\Users\tantr\.ssh\id_rsa_work
After these steps, you should have two sets of SSH keys in the .ssh
directory:
id_rsa_personal
and id_rsa_personal.pub
id_rsa_work
and id_rsa_work.pub
If you encounter this error, the directory does not exist. Create it with:
mkdir -p C:\Users\tantr\.ssh
Then, re-run the key generation commands above.
After generating your SSH keys, you’ll need to add them to your GitLab accounts.
1. Copy the public key for your personal account:
cat C:\Users\tantr\.ssh\id_rsa_personal.pub
2. Go to GitLab, log in to your personal account, navigate to Settings > SSH Keys, paste the public key, and save.
3. Repeat for the work account:
cat C:\Users\tantr\.ssh\id_rsa_work.pub
4. Log in to your work GitLab account, go to Settings > SSH Keys, paste the public key for your work account, and save.
Now that both accounts are configured on GitLab, you need to set up SSH to know which key to use for each account.
1. Open or create the SSH configuration file:
notepad C:\Users\tantr\.ssh\config
2. Add the following configuration:
# Personal GitLab account
Host gitlab-personal
HostName gitlab.com
User git
IdentityFile C:\Users\tantr\.ssh\id_rsa_personal
# Work GitLab account
Host gitlab-work
HostName gitlab.com
User git
IdentityFile C:\Users\tantr\.ssh\id_rsa_work
This configuration creates aliases (gitlab-personal
and gitlab-work
) so SSH can identify the appropriate key for each account.
If you see this error:
config
(not config.txt
).C:\Users\tantr\.ssh
.If both are correct, restart Git Bash or Command Prompt to refresh the SSH configuration.
Now that SSH is set up to recognize both accounts, update your Git repositories to use the aliases.
1. For personal projects, set the remote to use gitlab-personal
:
git remote set-url origin git@gitlab-personal:username/personal-repo.git
2. For work projects, set the remote to use gitlab-work
:
git remote set-url origin git@gitlab-work:username/work-repo.git
Finally, test to ensure everything works correctly.
1. Check the connection for each alias:
ssh -T git@gitlab-personal
You should see a message confirming that you’re authenticated with your personal GitLab account.
ssh -T git@gitlab-work
You should see a message confirming that you’re authenticated with your work account.
If you encounter this error:
Now, you can seamlessly switch between your personal and work GitLab accounts on the same Windows machine without needing to log in and out or manually switch SSH keys each time. Keeping separate keys for different accounts is a great practice to stay organized and secure while managing various projects.