Skip to content

Instantly share code, notes, and snippets.

@BenjaminWolfe
Last active February 16, 2021 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenjaminWolfe/8abf7b2d8edd3b99e20fa552d61edbaa to your computer and use it in GitHub Desktop.
Save BenjaminWolfe/8abf7b2d8edd3b99e20fa552d61edbaa to your computer and use it in GitHub Desktop.

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment