5

Is there any way to operate GitLab and GitHub accounts through SSH Key by using two different email address?

I Have two mail id, username@gmail.com and username@xyz.in and I want to push some projects to gitlab as well as some other projects to github and also I want to avoid password entering prompt in every push command. Is it possible?

James Z
  • 12,209
  • 10
  • 24
  • 44
JPG
  • 82,442
  • 19
  • 127
  • 206
  • To avoid ssh asking for passphrase use [ssh-agent](https://stackoverflow.com/a/10032655/7976758). – phd Aug 05 '17 at 10:21

2 Answers2

9

You could try working with ssh instead of https: upload your public ssh git to the server, and use an ssh url. For instance, for github you shouldn't have a url like

https://github.com/<username>/<repo>.git

but you should have

git@github.com:<username>/<repo>.git

--

Now, to use different ssh keys or username, you could try setting your ~/.ssh/config file to something like

Host           gitlab.com
HostName       gitlab.com
IdentityFile   ~/.ssh/gitlab
User           username@gmail.com

Host           github.com
HostName       github.com
IdentityFile   ~/.ssh/github
User           username@xyz.in

WhereIdentityFile are the path to your private ssh keys

gturri
  • 13,807
  • 9
  • 40
  • 57
2

One option for github is to use personal access tokens. Just log in to your account and go to your settings (click the dropdown beside your profile picture) then scroll to the bottom of the settings page and you will see a link to "Personal Access Tokens".

Then create a token with full access to repo (the other API access is not important if all you want to do is pull and push).

The token system was created to allow API access to github but it also work with HTTPS links.

Clone your repo using HTTPS instead of SSH (or if it's already SSH then just copy the HTTPS url and replace the SSH url in your .git/config file). Then edit your .git/config file and add the access token to the HTTPS url.

The syntax is standard Basic Auth syntax used by web browsers:

https://<YOUR_GITHUB_USERNAME>:<YOUR_ACCESS_TOKEN>@github.com

I use this to allow me to use github with multiple user accounts (for work, for my personal projects, for voluntary work etc.).

slebetman
  • 109,858
  • 19
  • 140
  • 171