Use Multiple Git Accounts on a Single System (Linux)
Suppose you have two accounts:
- account01
- account02
1. Open a terminal and run:
# GitHub (account01)
ssh-keygen -t ed25519 -C "account01-github" -f ~/.ssh/id_github_account01
# GitHub (account02)
ssh-keygen -t ed25519 -C "account02-github" -f ~/.ssh/id_github_account02
# GitLab (account01)
ssh-keygen -t ed25519 -C "account01-gitlab" -f ~/.ssh/id_gitlab_account01
# GitLab (account02)
ssh-keygen -t ed25519 -C "account02-gitlab" -f ~/.ssh/id_gitlab_account02
2. Start or use the SSH agent
If ssh-add -l already works in your session, you can skip this. Otherwise:
eval "$(ssh-agent -s)"
3. Create and modify ~/.ssh/config:
touch ~/.ssh/config
chmod 600 ~/.ssh/config
nano ~/.ssh/config
Add this configuration:
# GitHub (account01)
Host github.com-account01
HostName github.com
User git
IdentityFile ~/.ssh/id_github_account01
IdentitiesOnly yes
# GitHub (account02)
Host github.com-account02
HostName github.com
User git
IdentityFile ~/.ssh/id_github_account02
IdentitiesOnly yes
# GitLab (account01)
Host gitlab.com-account01
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_gitlab_account01
IdentitiesOnly yes
# GitLab (account02)
Host gitlab.com-account02
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_gitlab_account02
IdentitiesOnly yes
4. Add keys to the SSH agent:
ssh-add ~/.ssh/id_github_account01
ssh-add ~/.ssh/id_github_account02
ssh-add ~/.ssh/id_gitlab_account01
ssh-add ~/.ssh/id_gitlab_account02
ssh-add -l
5. Copy public keys (one at a time):
Wayland:
wl-copy < ~/.ssh/id_github_account01.pub
# repeat for each .pub file as needed
X11:
xclip -selection clipboard < ~/.ssh/id_github_account01.pub
Or use cat ~/.ssh/id_....pub and copy manually.
6. Test the connection:
ssh -T git@github.com-account01
ssh -T git@github.com-account02
ssh -T git@gitlab.com-account01
ssh -T git@gitlab.com-account02
7. Use SSH for account01
# New clone
git clone git@github.com-account01:git-username/repository.git
# Existing repo
git remote set-url origin git@github.com-account01:git-username/repository.git
Note:
Git’s default remote looks like git@github.com:git-username/repository.git. You must use your custom host alias, e.g. git@github.com-account01:..., so SSH picks the right key.
Useful tip:
git remote -vshows the current remote URLs