Say you want to access GitLab.com for work (using your work email address), and you also want to access it for personal reasons (using your personal email address), and you want to do both on the same laptop.
Generate two SSH keys and save them in your ~/.ssh
directory.
I'll call them work_key
and personal_key
.
As always with SSH keys in GitHub or GitLab,
you'll want to generate a public key for each and add it to the corresponding GitLab account.
Then point to both private keys in your ~/.ssh/config
file:
host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/work_key
host gitlab.com-personal
HostName gitlab.com
User git
IdentityFile ~/.ssh/personal_key
Notice that the HostName
is the same but the alias differs.
Now cd
in Terminal to the location of the repo,
and check the remotes:
(base) you@your-computer your-repo % git remote -v
origin git@gitlab.com:user-or-group/repo.git (fetch)
origin git@gitlab.com:user-or-group/repo.git (push)
If it looks like that it's because, like me, you only thought of this after cloning the repository. That's OK! Just change the remote:
git remote set-url origin git@gitlab.com-personal:user-or-group/repo.git
And now you can push to your repository!