In this tutorial you will learn how to manage multiple Git accounts (for example, a personal one and a company one) using different SSH keys and automatic per-folder configurations, all organized so you don't have to manually switch users or remotes.
##Goal
- Have a personal account (personal GitHub and GitLab) with the same SSH key.
- Have a company account (GitLab) with a different SSH key.
- Automate
user.nameanduser.emailbased on the folder. - Use differentiated remotes with the correct SSH key, especially for the company account.
##1. Create and Configure SSH Keys
First, we generate the keys (if you have already done this, you can skip this step).
###1.1. Generate Personal SSH Key
ssh-keygen -t rsa -b 4096 -C "your-personal-email@example.com" -f ~/.ssh/id_rsa
###1.2. Generate Company SSH Key
ssh-keygen -t rsa -b 4096 -C "your-company-email@example.com" -f ~/.ssh/id_rsa_company1
###1.3. Add Keys to the SSH Agent
# Personal
ssh-add ~/.ssh/id_rsa
# Company
ssh-add ~/.ssh/id_rsa_company1
###1.4. Upload Public Keys to the Platforms
- Personal GitHub/GitLab: Upload `~/.ssh/id_rsa.pub`.
- Company GitLab: Upload `~/.ssh/id_rsa_company1.pub`.
##2. Configure SSH to Use the Correct Keys
Edit (or create) the file `~/.ssh/config`:
nano ~/.ssh/config
Add:
# Personal Account (Personal GitHub and GitLab)
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa
# Company Account (Company GitLab)
Host gitlab.com-company1
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa_company1
Important: Use `gitlab.com-company1` as the host to distinguish it when cloning.
##3. Configure `user.name` and `user.email` per Folder
Edit or create the global `~/.gitconfig` file:
[user]
name = Your Personal Name
email = your-personal-email@example.com
[includeIf "gitdir:~/Developer/company1/"]
path = ~/.gitconfig-company1
Create the file `~/[CompanyFolder]/.gitconfig`:
[user]
name = Your Company Name
email = your-company-email@example.com
##4. Clone Repositories Correctly
###4.1. Personal Account (Personal GitHub and GitLab)
git clone git@github.com:user/repository.git
git clone git@gitlab.com:user/repository.git
###4.2. Company Account (Company GitLab using custom host)
git clone git@gitlab.com-company1:company/repository.git
##5. Fix Remotes for Old Projects (Automatic Script)
###5.1. Create the Script
#!/bin/bash
# Base path where company repositories are located
BASE_DIR=~/Developer/company1
# Find all .git directories
find "$BASE_DIR" -type d -name ".git" | while read gitdir; do
repo_dir=$(dirname "$gitdir")
echo "Fixing remote in: $repo_dir"
cd "$repo_dir"
# Get current remote
current_remote=$(git remote get-url origin)
# Replace if necessary
if [["$current_remote" == *"gitlab.com"* && "$current_remote" != *"gitlab.com-company1"*]]; then
new_remote=${current_remote/git@gitlab.com:/git@gitlab.com-company1:}
echo "Changing remote from: $current_remote to: $new_remote"
git remote set-url origin "$new_remote"
else
echo "Remote already correct or not gitlab: $current_remote"
fi
done
###5.2. Make it Executable
chmod +x fix-remotes.sh
###5.3. Run It
./fix-remotes.sh
##6. Automation When Cloning (Useful Alias)
Alias in `/.bashrc` or `/.zshrc`:
alias gitcompany1='git clone git@gitlab.com-company1:'
Usage:
gitcompany1 company/repository.git
With this configuration, you will have everything organized to work with multiple Git accounts without confusion.