2

I have two separate gitlab accounts, each with a unique SSH key and created with a different email. How can I push/pull normally (git push origin master / git pull origin master) from repos in both accounts? Do I have to configure both ~/.gitconfig and ~/.ssh/config?

1st gitlab account (username1, email1@gmail.com)

2nd gitlab account (username2, email2@gmail.com)

~/.ssh/config

 Host gitlab.com
    User username1
    IdentityFile ~/.ssh/id_rsa

    Host gitlab.com
    User username2
    IdentityFile ~/.ssh/id_rsa_

~/.gitconfig

[user]
name = Bob
email = email1@gmail.com
username = username1

Problem is I can only pull from one repo with this configuration and not both. How can make it work for both?

user3226932
  • 2,042
  • 6
  • 39
  • 76

1 Answers1

3

You can do this by editing yout ~/.ssh/config

Here yo add two sections for each account one. Like the example.

#user1 account
Host gitlab.com-user1
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/gitlab-user1

#user2 account
Host gitlab.com-user2
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/gitlab-user2

Then you have to change the remote url like the following.

Use Account 1 git@gitlab.com-user1:<yourProjectToClone>

Use Account 2 git@gitlab.com-user2:<yourProjectToClone>

Hope this helps.

sweting
  • 390
  • 1
  • 8
  • nice that worked, but does this mean i'll have to always change the remote url for each repo in my gitlab account? – user3226932 Feb 12 '18 at 23:17
  • @user3226932 You have to change the remote url in your local clones not in gitlab direct. You can try if you only change the url for one account and change the ssh-config of one account to Host gitlab.com – sweting Feb 12 '18 at 23:24