0

I do have in my ~/.ssh/config file two different GitLab accounts. Both are named on Host different and refers on Hostname to gitlab.com.

Every time I switch between coding projects of the different GitLab accounts, I have to do:

$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/<ssh-key-for-GitLab-account-of-actual-project

I've read here What eval and ssh-agent commands do?, that it has something to do with Docker, but in my case it's only on my local machine.

Is there a way, avoiding insert this two commands all the time?

4
  • medium.com/uncaught-exception/…
    – Javlon
    Commented Sep 12, 2022 at 11:51
  • Maybe this will shed some light on the issue: rabexc.org/posts/pitfalls-of-ssh-agents
    – gronostaj
    Commented Sep 12, 2022 at 12:00
  • How do you switch between the projects?
    – harrymc
    Commented Sep 12, 2022 at 12:26
  • ..I've not forgotten you <3 I'll try your solutions on the weekend - both are very interesting!! I switch between the projects sometimes in iTerm2 (a Mac terminal emulator), sometimes with JetBrains PhpStorm IDE.
    – Manny
    Commented Sep 15, 2022 at 6:40

1 Answer 1

0

I've tried the way @JavIon has provided:
https://medium.com/uncaught-exception/setting-up-multiple-gitlab-accounts-82b70e88c437

On my system, I've done some fine tuning.

1st step:
Go into your local directories where you use Git. In my case, I use Git with GitLab.
In my work directory are some git repositories and some regular files and directories - same in my private directory.
I've created in each of these directories a .gitignore file.

Work:

#------------------------------------------#
#         .gitconfig GitLab Work           #
#------------------------------------------#


[user]
    name     = My name for commits at work
    email    = [email protected]
    username = Work GitLab username

Private:

#------------------------------------------#
#        .gitconfig GitLab Private         #
#------------------------------------------#


[user]
    name     = My name for commits at private
    email    = [email protected]
    username = Private GitLab username

2nd step:
Go into your home directory and open .gitconfig (if there is none, create one).
Then include the .gitconfig files from your directories, except that in home directory.

~/.gitconfig:

#-----------------------------------#
#        .gitconfig Global          #
#-----------------------------------#


#----------------- Work GitLab
[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig


#----------------- Private GitLab
[includeIf "gitdir:/~/private/"]
    path = ~/private/.gitconfig

3rd step:
Go into your local Git repositories and add in all of them the specific SSH key.

ssh-add ~/.ssh/<your key>

For me, that has worked without logout or reboot.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .